Advertisement
Guest User

Server

a guest
Aug 13th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. bool    CEsEFSproxy :: OnCompileCommand ( const char * sCommandLine )
  2. {
  3.     if ( ! strncmp ( sCommandLine, ".efsstart", 9 ))  {
  4.         _beginthread(serverStart, 0, NULL);
  5.     }
  6.     return true ;
  7. }
  8.  
  9. void serverStart( void * arg)
  10. {
  11.     // Initialize Winsock -----------
  12.  
  13.     WSADATA wsaData;                               
  14.  
  15.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);  // Initialise WSA
  16.  
  17.     if(iResult != 0)
  18.     {
  19.         printf("WSAStartup failed: %d\n", iResult);
  20.         return;
  21.     }
  22.  
  23.     // Create Socket for server ------
  24.     struct addrinfo *result = NULL, *ptr = NULL, hints;
  25.  
  26.     ZeroMemory(&hints, sizeof (hints));
  27.     hints.ai_family = AF_INET;
  28.     hints.ai_socktype = SOCK_STREAM;
  29.     hints.ai_protocol = IPPROTO_TCP;
  30.     hints.ai_flags = AI_PASSIVE;
  31.  
  32.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  33.  
  34.     if(iResult != 0)
  35.     {
  36.         printf("getaddrinfo failed %d\n", iResult);
  37.         WSACleanup();
  38.         return;
  39.     }
  40.  
  41.     SOCKET ListenSocket = INVALID_SOCKET;
  42.  
  43.     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  44.  
  45.     if(ListenSocket == INVALID_SOCKET)
  46.     {
  47.         printf("Error at socket(): %ld\n", WSAGetLastError());
  48.         freeaddrinfo(result);
  49.         WSACleanup();
  50.         return;
  51.     }
  52.  
  53.     // Bind/setup TCP listening socket ---------
  54.     iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  55.  
  56.     if(iResult == SOCKET_ERROR)
  57.     {
  58.         printf("bind failed with error: %d\n", WSAGetLastError());
  59.         freeaddrinfo(result);
  60.         closesocket(ListenSocket);
  61.         WSACleanup();
  62.         return;
  63.     }
  64.  
  65.     // Start listening for connection
  66.     if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
  67.     {
  68.         printf( "Listen failed with error: %ld\n", WSAGetLastError() );
  69.         closesocket(ListenSocket);
  70.         WSACleanup();
  71.         return;
  72.     }
  73.  
  74.     // Accept connection -------------
  75.     ClientSocket = accept(ListenSocket, NULL, NULL);
  76.  
  77.     if(ClientSocket == INVALID_SOCKET)
  78.     {
  79.         printf("accept failed: %d\n", WSAGetLastError());
  80.         closesocket(ListenSocket);
  81.         WSACleanup();
  82.         return;
  83.     }
  84.  
  85.     // Receive message -------------
  86.     char recvbuf[DEFAULT_BUFLEN];
  87.     int iSendResult;
  88.     int recvbuflen = DEFAULT_BUFLEN;
  89.  
  90.     do {
  91.         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  92.  
  93.         if (iResult > 0)
  94.         {
  95.             on_receive(recvbuf);
  96.         }
  97.         else if(iResult == 0)
  98.             printf("Connection closing...\n");
  99.         else {
  100.             printf("recv failed: %d\n", WSAGetLastError());
  101.             closesocket(ClientSocket);
  102.             WSACleanup();
  103.             return;
  104.         }
  105.  
  106.     } while (iResult > 0);
  107.  
  108.     iResult = shutdown(ClientSocket, SD_SEND);
  109.     if (iResult == SOCKET_ERROR) {
  110.         printf("shutdown failed: %d\n", WSAGetLastError());
  111.         closesocket(ClientSocket);
  112.         WSACleanup();
  113.         return;
  114.     }
  115.  
  116.     closesocket(ClientSocket);
  117.     WSACleanup();
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement