Advertisement
atm959

Server Side...?

Aug 6th, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #ifndef WIN32_LEAN_AND_MEAN
  2. #define WIN32_LEAN_AND_MEAN
  3. #endif
  4.  
  5. #include <WinSock2.h>
  6. #include <WS2tcpip.h>
  7. #include <stdio.h>
  8.  
  9. #pragma comment(lib, "Ws2_32.lib")
  10.  
  11. #define DEFAULT_PORT "27015"
  12.  
  13. WSADATA wsaData;
  14.  
  15. int main(int argc, char* argv[]) {
  16.     int iResult;
  17.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  18.     if (iResult != 0) {
  19.         printf("WSAStartup failed: %d\n", iResult);
  20.         return 1;
  21.     }
  22.  
  23.     struct addrinfo *result = NULL, *ptr = NULL, hints;
  24.     ZeroMemory(&hints, sizeof(hints));
  25.     hints.ai_family = AF_INET;
  26.     hints.ai_socktype = SOCK_STREAM;
  27.     hints.ai_protocol = IPPROTO_TCP;
  28.     hints.ai_flags = AI_PASSIVE;
  29.  
  30.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  31.     if (iResult != 0) {
  32.         printf("getaddrinfo failed: %d\n", iResult);
  33.         WSACleanup();
  34.         return 1;
  35.     }
  36.  
  37.     SOCKET ListenSocket = INVALID_SOCKET;
  38.     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  39.     if (ListenSocket == INVALID_SOCKET) {
  40.         printf("Error at socket(): %ld\n", WSAGetLastError());
  41.         freeaddrinfo(result);
  42.         WSACleanup();
  43.         return 1;
  44.     }
  45.  
  46.     // Setup the TCP listening socket
  47.     iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  48.     if (iResult == SOCKET_ERROR) {
  49.         printf("bind failed with error: %d\n", WSAGetLastError());
  50.         freeaddrinfo(result);
  51.         closesocket(ListenSocket);
  52.         WSACleanup();
  53.         return 1;
  54.     }
  55.     freeaddrinfo(result);
  56.  
  57.     if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
  58.         printf("Listen failed with error: %ld\n", WSAGetLastError());
  59.         closesocket(ListenSocket);
  60.         WSACleanup();
  61.         return 1;
  62.     }
  63.  
  64.     SOCKET ClientSocket;
  65.     ClientSocket = INVALID_SOCKET;
  66.  
  67.     // Accept a client socket
  68.     ClientSocket = accept(ListenSocket, NULL, NULL);
  69.     if (ClientSocket == INVALID_SOCKET) {
  70.         printf("accept failed: %d\n", WSAGetLastError());
  71.         closesocket(ListenSocket);
  72.         WSACleanup();
  73.         return 1;
  74.     }
  75.  
  76. #define DEFAULT_BUFLEN 512
  77.  
  78.     char recvbuf[DEFAULT_BUFLEN];
  79.     int iSendResult;
  80.     int recvbuflen = DEFAULT_BUFLEN;
  81.  
  82.     // Receive until the peer shuts down the connection
  83.     do {
  84.  
  85.         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  86.         if (iResult > 0) {
  87.             printf("Bytes received: %d\n", iResult);
  88.  
  89.             // Echo the buffer back to the sender
  90.             iSendResult = send(ClientSocket, recvbuf, iResult, 0);
  91.             if (iSendResult == SOCKET_ERROR) {
  92.                 printf("send failed: %d\n", WSAGetLastError());
  93.                 closesocket(ClientSocket);
  94.                 WSACleanup();
  95.                 return 1;
  96.             }
  97.             printf("Bytes sent: %d\n", iSendResult);
  98.         } else if (iResult == 0)
  99.             printf("Connection closing...\n");
  100.         else {
  101.             printf("recv failed: %d\n", WSAGetLastError());
  102.             closesocket(ClientSocket);
  103.             WSACleanup();
  104.             return 1;
  105.         }
  106.  
  107.     } while (iResult > 0);
  108.  
  109.     WSACleanup();
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement