Advertisement
captmicro

winsock2 client/server

May 10th, 2010
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. DWORD WINAPI networkCl(void *userData)
  2. {
  3.     char server[256] = {0};
  4.     strcpy(server, (char*)userData);
  5.  
  6.     int iError = 0;
  7.     struct addrinfo *result = NULL, hints;
  8.  
  9.     memset(&hints, 0, sizeof(hints));
  10.     hints.ai_family = AF_INET;
  11.     hints.ai_socktype = SOCK_STREAM;
  12.     hints.ai_protocol = IPPROTO_TCP;
  13.  
  14.     iError = getaddrinfo(server, "9904", &hints, &result);
  15.     if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
  16.  
  17.     thisSock = INVALID_SOCKET;
  18.     thisSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  19.     if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
  20.  
  21.     iError = connect(thisSock, result->ai_addr, (int)result->ai_addr);
  22.     freeaddrinfo(result);
  23.     if (iError == SOCKET_ERROR) { printf("connect failed: %d\n", WSAGetLastError()); closesocket(thisSock); }
  24.     if (thisSock == INVALID_SOCKET) { printf("unable to connect to server\n"); WSACleanup(); return 1; }
  25.  
  26.     sendPacket(IDBYTE_SETNAME, local.name);
  27.  
  28.     char recvData[256] = {0}; int recvBytes = 0;
  29.     do
  30.     {
  31.         recvBytes = recv(thisSock, recvData, sizeof(recvData), 0);
  32.         if (recvBytes > 0)
  33.         {
  34.             char idByte = recvData[0];
  35.             char *rData = &recvData[1];
  36.             printf("Packet received: ");
  37.             for (int i = 0; i < strlen(recvData); i++)
  38.                 printf("0x%02X ", recvData[i]);
  39.             printf("\n");
  40.             if (idByte == IDBYTE_SETNAME)
  41.             {
  42.                 strcpy(other.name, rData);
  43.                 chatPrint("other changed name to %s", rData);
  44.             }
  45.             else if (idByte == IDBYTE_CHATMSG)
  46.             {
  47.                 chatPrint("%s", rData);
  48.             }
  49.             else if (idByte == IDBYTE_MOVEPOS)
  50.             {
  51.                 float deltaX = 0;
  52.                 deltaX = atof(rData);
  53.                 other.sprite.Move((float)deltaX, 0);
  54.             }
  55.             else if (idByte == IDBYTE_SHOOTBT)
  56.             {
  57.                 actionSounds[0].Play();
  58.             }
  59.         }
  60.     } while (recvBytes > 0);
  61.     closesocket(thisSock);
  62.     WSACleanup();
  63.    
  64.     printf("End of client function.\n");
  65.     return 0;
  66. }
  67.  
  68. DWORD WINAPI networkSv(void *userData)
  69. {
  70.     int iError = 0;
  71.     struct addrinfo *result = NULL, hints;
  72.  
  73.     memset(&hints, 0, sizeof(hints));
  74.     hints.ai_family = AF_INET;
  75.     hints.ai_socktype = SOCK_STREAM;
  76.     hints.ai_protocol = IPPROTO_TCP;
  77.     hints.ai_flags = AI_PASSIVE;
  78.  
  79.     iError = getaddrinfo(NULL, "9904", &hints, &result);
  80.     if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
  81.  
  82.     thisSock = INVALID_SOCKET;
  83.     thisSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  84.     if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
  85.  
  86.     iError = bind(thisSock, result->ai_addr, (int)result->ai_addr);
  87.     if (iError == SOCKET_ERROR) { printf("bind failed: %d\n", WSAGetLastError()); freeaddrinfo(result); closesocket(thisSock); WSACleanup(); return 1; }
  88.  
  89.     if (listen(thisSock, SOMAXCONN) == SOCKET_ERROR)
  90.     {
  91.         printf("listen error: %d\n", WSAGetLastError());
  92.         closesocket(thisSock);
  93.         WSACleanup();
  94.         return 1;
  95.     }
  96.  
  97.     clSock = INVALID_SOCKET;
  98.     clSock = accept(thisSock, NULL, NULL);
  99.     if (clSock == INVALID_SOCKET)
  100.     {
  101.         printf("accept error: %d\n", WSAGetLastError());
  102.         closesocket(thisSock);
  103.         WSACleanup();
  104.         return 1;
  105.     }
  106.  
  107.     sendPacket(IDBYTE_SETNAME, local.name);
  108.  
  109.     char recvData[256] = {0}; int recvBytes = 0;
  110.     do
  111.     {
  112.         recvBytes = recv(clSock, recvData, sizeof(recvData), 0);
  113.         if (recvBytes > 0)
  114.         {
  115.             char idByte = recvData[0];
  116.             char *rData = &recvData[1];
  117.             printf("Packet received: ");
  118.             for (int i = 0; i < strlen(recvData); i++)
  119.                 printf("0x%02X ", recvData[i]);
  120.             printf("\n");
  121.             if (idByte == IDBYTE_SETNAME)
  122.             {
  123.                 strcpy(other.name, rData);
  124.                 chatPrint("other changed name to %s", rData);
  125.             }
  126.             else if (idByte == IDBYTE_CHATMSG)
  127.             {
  128.                 chatPrint("%s", rData);
  129.             }
  130.             else if (idByte == IDBYTE_MOVEPOS)
  131.             {
  132.                 float deltaX = 0;
  133.                 deltaX = atof(rData);
  134.                 other.sprite.Move((float)deltaX, 0);
  135.             }
  136.             else if (idByte == IDBYTE_SHOOTBT)
  137.             {
  138.                 actionSounds[0].Play();
  139.             }
  140.         }
  141.     } while (recvBytes > 0);
  142.     closesocket(thisSock);
  143.     WSACleanup();
  144.  
  145.     printf("End of server function.\n");
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement