captmicro

winsock2 client/server init

May 10th, 2010
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 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;
  8.     struct addrinfo hints;
  9.     memset(&result, 0, sizeof(result));
  10.     memset(&hints, 0, sizeof(hints));
  11.     hints.ai_family = AF_INET;
  12.     hints.ai_socktype = SOCK_STREAM;
  13.     hints.ai_protocol = IPPROTO_TCP;
  14.  
  15.     iError = getaddrinfo(server, "9904", &hints, (PADDRINFOA*)&result);
  16.     if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
  17.  
  18.     thisSock = INVALID_SOCKET;
  19.     thisSock = socket(result.ai_family, result.ai_socktype, result.ai_protocol);
  20.     if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
  21.  
  22.     iError = connect(thisSock, result.ai_addr, (int)result.ai_addr);
  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.  
  27.  
  28. DWORD WINAPI networkSv(void *userData)
  29. {
  30.     int iError = 0;
  31.     struct addrinfo result;
  32.     struct addrinfo hints;
  33.     memset(&result, 0, sizeof(result));
  34.     memset(&hints, 0, sizeof(hints));
  35.     hints.ai_family = AF_INET;
  36.     hints.ai_socktype = SOCK_STREAM;
  37.     hints.ai_protocol = IPPROTO_TCP;
  38.     hints.ai_flags = AI_PASSIVE;
  39.  
  40.     iError = getaddrinfo(NULL, "9904", &hints, (PADDRINFOA*)&result);
  41.     if (iError != 0) { printf("getaddrinfo failed: %d\n", iError); WSACleanup(); return 0; }
  42.  
  43.     thisSock = INVALID_SOCKET;
  44.     thisSock = socket(result.ai_family, result.ai_socktype, result.ai_protocol);
  45.     if (thisSock == INVALID_SOCKET) { printf("socket failed: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
  46.  
  47.     iError = bind(thisSock, result.ai_addr, (int)result.ai_addr);
  48.     if (iError == SOCKET_ERROR) { printf("bind failed: %d\n", WSAGetLastError()); closesocket(thisSock); WSACleanup(); return 1; }
  49.  
  50.     if (listen(thisSock, SOMAXCONN) == SOCKET_ERROR)
  51.     {
  52.         printf("listen error: %d\n", WSAGetLastError());
  53.         closesocket(thisSock);
  54.         WSACleanup();
  55.         return 1;
  56.     }
  57.  
  58.     clSock = INVALID_SOCKET;
  59.     clSock = accept(thisSock, NULL, NULL);
  60.     if (clSock == INVALID_SOCKET)
  61.     {
  62.         printf("accept error: %d\n", WSAGetLastError());
  63.         closesocket(thisSock);
  64.         WSACleanup();
  65.         return 1;
  66.     }
Add Comment
Please, Sign In to add comment