46 #ifndef __TI_COMPILER_VERSION__ 59 static int socket_ref_count = 0;
65 if (socket_ref_count > 1)
68 WORD wVersionRequested;
72 wVersionRequested = MAKEWORD(2, 0);
73 wsaerr = WSAStartup(wVersionRequested, &wsaData);
77 printf(
"Error: couldn't initialize WinSock!\n");
83 void ShutdownWinSock()
86 if (socket_ref_count > 0)
92 #elif defined(__TI_COMPILER_VERSION__) 99 #include <sys/socket.h> 100 #include <sys/types.h> 102 #include <netinet/ip.h> 103 #include <netinet/tcp.h> 129 SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
130 if (sock == INVALID_SOCKET)
143 if(setsockopt(sock, SOL_SOCKET, SO_LINGER, (
const char*)&li,
sizeof(li)) == -1)
145 printf(
"setting linger option failed\n");
152 if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
154 printf(
"setting non-blocking option failed\n");
159 struct sockaddr_in serv_addr;
160 memset((
char *)&serv_addr, 0,
sizeof(serv_addr));
162 serv_addr.sin_family = AF_INET;
165 memcpy((
char *)&serv_addr.sin_addr.s_addr, ip, 4);
169 serv_addr.sin_addr.s_addr = INADDR_ANY;
171 serv_addr.sin_port =
htons(port);
173 if (bind(sock, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
182 #elif defined(__TI_COMPILER_VERSION__) 204 uint_32 opt_value = TRUE;
205 error = setsockopt(
m_socket, SOL_TCP, OPT_SEND_NOWAIT, &opt_value,
sizeof(opt_value));
206 if (error != VCRT_OK)
208 printf(
"Error, setsockopt(OPT_SEND_NOWAIT) failed with error code 0x%x\n", error);
213 sockaddr_in serv_addr;
214 memset( &serv_addr, 0,
sizeof(serv_addr));
215 serv_addr.sin_family = AF_INET;
219 memcpy((
char *)&serv_addr.sin_addr.s_addr, ip, 4);
226 serv_addr.sin_addr.s_addr = INADDR_ANY;
228 serv_addr.sin_port = port;
230 error = bind(
m_socket, &serv_addr,
sizeof(serv_addr));
233 printf(
"\nsocket bind failed, error code: 0x%x\n", error);
241 printf(
"\nsocket listen failed, error code: 0x%x\n", error);
248 m_socket = socket(AF_INET, SOCK_STREAM, 0);
260 if(setsockopt(
m_socket, SOL_SOCKET, SO_LINGER, &li,
sizeof(li)) == -1)
262 printf(
"setting linger option failed\n");
268 if (fcntl(
m_socket, F_SETFL, O_NONBLOCK) == -1)
270 printf(
"setting non-blocking option failed\n");
275 struct sockaddr_in serv_addr;
276 bzero((
char *)&serv_addr,
sizeof(serv_addr));
278 serv_addr.sin_family = AF_INET;
281 bcopy(ip, (
char *)&serv_addr.sin_addr.s_addr, 4);
285 serv_addr.sin_addr.s_addr = INADDR_ANY;
287 serv_addr.sin_port =
htons(port);
289 if (bind(
m_socket, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
309 struct sockaddr_in cli_addr;
310 int clilen =
sizeof(cli_addr);
312 SOCKET new_socket = accept(sock, (
struct sockaddr *) &cli_addr, &clilen);
313 if (new_socket == INVALID_SOCKET)
320 if (ioctlsocket(new_socket, FIONBIO, &v) == SOCKET_ERROR)
322 printf(
"setting non-blocking option failed\n");
329 if (setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, (
const char*)&v2,
sizeof(v2)) == -1)
331 printf(
"newly accepted socket couldn't be set to no delay mode\n");
336 memcpy(ip, (
char *) &cli_addr.sin_addr.s_addr, 4);
344 #elif defined(__TI_COMPILER_VERSION__) 349 error = VCRT_selectset(&
m_socket, 1, (uint_32)-1);
355 if (error==VCRT_SOCKET_ERROR)
357 printf(
"Error in accept(): VCRT_selectset failed, error code 0x%x\n", VCRT_geterror(
m_socket));
362 printf(
"Error in accept(): VCRT_selectset returned invalid socket");
367 sockaddr_in cli_addr;
368 uint_16 clilen =
sizeof(cli_addr);
370 int new_socket = accept(
m_socket, &cli_addr, &clilen);
371 if (new_socket == VCRT_SOCKET_ERROR)
374 if (error == VCRT_OK)
376 printf(
"\nAccept: Connection reset by peer");
380 printf(
"Error, accept() failed with error code 0x%x\n", error);
396 error = setsockopt(
m_socket, SOL_TCP, OPT_SEND_NOWAIT, &opt_value,
sizeof(opt_value));
397 if (error != VCRT_OK)
399 printf(
"Error, setsockopt(OPT_SEND_NOWAIT) failed with error code 0x%x\n", error);
407 error = setsockopt(
m_socket, SOL_TCP, OPT_NO_NAGLE_ALGORITHM, &opt_value,
sizeof(opt_value));
408 if (error != VCRT_OK)
410 printf(
"Error, setsockopt(OPT_NO_NAGLE_ALGORITHM) failed with error code 0x%x\n", error);
421 memcpy( ip, &cli_addr.sin_addr.s_addr, 4);
430 struct sockaddr_in cli_addr;
431 socklen_t clilen =
sizeof(cli_addr);
433 int new_socket = accept(
m_socket, (
struct sockaddr *) &cli_addr, &clilen);
440 if (fcntl(new_socket, F_SETFL, O_NONBLOCK) == -1)
442 printf(
"newly accepted socket couldn't be set to non blocking mode\n");
447 if (setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, &v,
sizeof(v)) == -1)
449 printf(
"newly accepted socket couldn't be set to no delay mode\n");
454 bcopy((
char *)&cli_addr.sin_addr.s_addr, ip, 4);
469 SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
470 if (sock == INVALID_SOCKET)
478 struct sockaddr_in serv_addr;
479 memset((
char *)&serv_addr, 0,
sizeof(serv_addr));
481 serv_addr.sin_family = AF_INET;
482 memcpy((
char *)&serv_addr.sin_addr.s_addr, ip, 4);
483 serv_addr.sin_port =
htons(port);
485 if (connect(sock, (
const sockaddr*)&serv_addr,
sizeof(serv_addr)) < 0)
493 if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
495 printf(
"setting non-blocking option failed\n");
502 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (
const char*)&v2,
sizeof(v2)) == -1)
504 printf(
"socket couldn't be set to no delay mode\n");
509 #elif defined(__TI_COMPILER_VERSION__) 515 printf(
"Error in Open(): socket_stream() failed\n");
520 sockaddr_in serv_addr;
521 memset( &serv_addr, 0,
sizeof(serv_addr));
522 serv_addr.sin_family = AF_INET;
525 memcpy(&(serv_addr.sin_addr.s_addr), ip, 4);
530 serv_addr.sin_port = port;
534 error = connect(
m_socket, &serv_addr,
sizeof(serv_addr));
535 if (error != VCRT_OK)
537 printf(
"Error in Open(): connect failed, error code 0x%x\n", error);
551 error = setsockopt(
m_socket, SOL_TCP, OPT_SEND_NOWAIT, &opt_value,
sizeof(opt_value));
552 if (error != VCRT_OK)
554 printf(
"Error, setsockopt(OPT_SEND_NOWAIT) failed with error code 0x%x\n", error);
563 error = setsockopt(
m_socket, SOL_TCP, OPT_NO_NAGLE_ALGORITHM, &opt_value,
sizeof(opt_value));
564 if (error != VCRT_OK)
566 printf(
"Error, setsockopt(OPT_NO_NAGLE_ALGORITHM) failed with error code 0x%x\n", error);
574 m_socket = socket(AF_INET, SOCK_STREAM, 0);
581 struct sockaddr_in serv_addr;
582 bzero((
char *)&serv_addr,
sizeof(serv_addr));
584 serv_addr.sin_family = AF_INET;
585 bcopy(ip, (
char *)&serv_addr.sin_addr.s_addr, 4);
586 serv_addr.sin_port =
htons(port);
588 if (connect(
m_socket, (
const sockaddr*)&serv_addr,
sizeof(serv_addr)) < 0)
595 if (fcntl(
m_socket, F_SETFL, O_NONBLOCK) == -1)
603 if (setsockopt(
m_socket, IPPROTO_TCP, TCP_NODELAY, &v,
sizeof(v)) == -1)
624 #elif defined(__TI_COMPILER_VERSION__) 628 uint_32 error = shutdown(
m_socket, FLAG_ABORT_CONNECTION);
631 printf(
"\nsocket shutdown failed, error code: 0x%x\n", error);
648 unsigned char *ptr = (
unsigned char*) pData;
651 int s = send(sock, (
const char *) ptr, nBytes, 0);
652 if (s == SOCKET_ERROR)
654 int errnum = WSAGetLastError();
655 if (errnum == WSAEWOULDBLOCK)
669 #elif defined(__TI_COMPILER_VERSION__) 671 char *ptr = (
char *) pData;
679 if (count==VCRT_ERROR)
681 printf(
"Error in Send(): send failed, error code 0x%x\n", VCRT_geterror(
m_socket));
691 unsigned char *ptr = (
unsigned char *) pData;
698 if (errnum == EAGAIN)
727 unsigned char *ptr = (
unsigned char*) pData;
730 int s = recv(sock, (
char *) ptr, nMaxBytes, 0);
731 if (s == SOCKET_ERROR)
733 int errnum = WSAGetLastError();
734 if (errnum == WSAEWOULDBLOCK)
752 int size = recv(sock, (
char *) pData, nMaxBytes, 0);
753 if (size == SOCKET_ERROR)
755 int errnum = WSAGetLastError();
756 if (errnum == WSAEWOULDBLOCK)
771 #elif defined(__TI_COMPILER_VERSION__) 776 uint_32 opt_value = bWait? FALSE : TRUE;
777 uint_32 error = setsockopt(
m_socket, SOL_TCP, OPT_RECEIVE_NOWAIT, &opt_value,
sizeof(opt_value));
778 if (error != VCRT_OK)
780 printf(
"Error in Recv(), setsockopt(OPT_RECEIVE_NOWAIT) failed with error code 0x%x\n", error);
787 char *ptr = (
char *) pData;
792 if (count == VCRT_ERROR)
794 printf(
"Error in Recv(): recv failed, error code 0x%x\n", VCRT_geterror(
m_socket));
810 if (count == VCRT_ERROR)
812 printf(
"Error in Recv(): recv failed, error code 0x%x\n", VCRT_geterror(
m_socket));
826 unsigned char *ptr = (
unsigned char *) pData;
829 int s = (int) recv(
m_socket, ptr, nMaxBytes, 0);
833 if (errnum == EAGAIN)
856 if (errnum == EAGAIN)
int Recv(void *pData, int nMaxBytes, bool bWait=false)
GLfloat GLfloat GLfloat v2
bool Listen(const unsigned char *ip=0, int port=0)
CTCPSocket * Accept(unsigned char *ip=0)
uint32_t revert_byte_order(uint32_t dword)
bool Open(const unsigned char *ip, int port)
bool Send(const void *pData, int nBytes)
GLuint GLuint GLsizei count
uint16_t htons(uint16_t hostshort)