26 #define COPY_LAST_SOCKET_ERROR() sprintf(_errorBuffer, "%d", WSAGetLastError())
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
36 #define COPY_LAST_SOCKET_ERROR() sprintf(_errorBuffer, "%d", errno)
37 #define INVALID_SOCKET -1
38 #define SOCKET_ERROR -1
43 namespace OpenSkyNet {
52 int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
54 if (result != NO_ERROR) {
55 sprintf(errorBuffer_,
"%d", result);
72 _socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
82 bool connectTo(
const char* ipAddr_,
unsigned int port_) {
84 service.sin_family = AF_INET;
85 service.sin_addr.s_addr = inet_addr(ipAddr_);
86 service.sin_port = htons(port_);
88 int result = connect(_socket, (sockaddr*)&service,
sizeof(service));
99 bool listenTo(
const char* ipAddr_,
unsigned int port_) {
101 service.sin_family = AF_INET;
102 service.sin_addr.s_addr = inet_addr(ipAddr_);
103 service.sin_port = htons(port_);
105 int result = bind(_socket, (sockaddr*)&service,
sizeof(service));
112 result = listen(_socket, SOMAXCONN);
124 _acceptSocket = accept(_socket, NULL, NULL);
138 int result = closesocket(_socket);
140 int result = shutdown(_socket, SHUT_RDWR);
147 result = close(_socket);
156 if (_acceptSocket !=
INVALID_SOCKET && _acceptSocket != _socket) {
158 int result = closesocket(_acceptSocket);
160 int result = shutdown(_acceptSocket, SHUT_RDWR);
167 result = close(_acceptSocket);
178 _isConnected =
false;
187 result = send(_socket, buffer_, static_cast<int>(bufferLen_), 0);
190 result = send(_acceptSocket, buffer_, static_cast<int>(bufferLen_), 0);
201 sprintf(_errorBuffer,
"Not Connected");
210 result = recv(_socket, buffer_, static_cast<int>(bufferLen_), 0);
213 result = recv(_acceptSocket, buffer_, static_cast<int>(bufferLen_), 0);
217 memset(buffer_, 0, bufferLen_);
228 sprintf(_errorBuffer,
"Not Connected");
249 memcpy(buffer_, &val_, size);
259 T val = *
reinterpret_cast<T*
>(buffer_);
bool closeConnection()
Definition: USocket.h:135
const char * getLastErrorString() const
Definition: USocket.h:236
bool create()
Definition: USocket.h:71
void serialize(T val_, char *&buffer_, OpenSkyNet::Utils::uint &bufferLen_)
Definition: USocket.h:246
bool isConnectedAsClient() const
Definition: USocket.h:234
static bool init(char(&errorBuffer_)[MAX_STRING_SIZE])
Definition: USocket.h:48
#define MAX_STRING_SIZE
Definition: UTypes.h:33
bool sendData(const char *buffer_, OpenSkyNet::Utils::uint bufferLen_)
Definition: USocket.h:182
#define COPY_LAST_SOCKET_ERROR()
Definition: USocket.h:36
unsigned int uint
Definition: UTypes.h:39
bool isConnectedAsServer() const
Definition: USocket.h:233
#define INVALID_SOCKET
Definition: USocket.h:37
T assemble(char *&buffer_, OpenSkyNet::Utils::uint &bufferLen_)
Definition: USocket.h:256
static void shutDown()
Definition: USocket.h:63
bool isConnected() const
Definition: USocket.h:232
Socket()
Definition: USocket.h:69
int SOCKET
Definition: USocket.h:34
int recvData(char *buffer_, OpenSkyNet::Utils::uint bufferLen_)
Definition: USocket.h:205
#define SOCKET_ERROR
Definition: USocket.h:38
bool connectTo(const char *ipAddr_, unsigned int port_)
Definition: USocket.h:82
bool acceptConnection()
Definition: USocket.h:122
bool listenTo(const char *ipAddr_, unsigned int port_)
Definition: USocket.h:99