xXTurner

server WInSocks2

Aug 22nd, 2021
762
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This should be placed before #include <Windows.h> if the windows include is used
  2. #ifndef WIN32_LEAN_AND_MEAN
  3. #define WIN32_LEAN_AND_MEAN
  4. #endif
  5.  
  6. #include <cstdio>
  7.  
  8. #include <Windows.h>
  9. #include <WinSock2.h>
  10. #include <WS2tcpip.h>
  11.  
  12. /// TEST CODE /// REMOVE AFTER TESTING WAS DONE ///
  13. int main()
  14. {
  15.     WSADATA wsaData;
  16.     int iResult;
  17.  
  18.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  19.     if (iResult != 0)
  20.     {
  21.         printf("WSAStartup failed: %d\n", iResult);
  22.         return 1;
  23.     }
  24.  
  25.     struct addrinfo* result = NULL, * ptr = NULL, hints;
  26.  
  27.     ZeroMemory(&hints, sizeof(hints));
  28.     hints.ai_family = AF_INET;
  29.     hints.ai_socktype = SOCK_STREAM;
  30.     hints.ai_protocol = IPPROTO_TCP;
  31.     hints.ai_flags = AI_PASSIVE;
  32.  
  33.     // Resolve the local address and port to be used by the server
  34.     iResult = getaddrinfo("0.0.0.0", "1337", &hints, &result);
  35.     if (iResult != 0)
  36.     {
  37.         printf("getaddrinfo failed: %d\n", iResult);
  38.         WSACleanup();
  39.         return 1;
  40.     }
  41.  
  42.     SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  43.    
  44.     if (ListenSocket == INVALID_SOCKET)
  45.     {
  46.         printf("Error at socket(): %ld\n", WSAGetLastError());
  47.         freeaddrinfo(result);
  48.         WSACleanup();
  49.     }
  50.  
  51.     iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  52.     if (iResult == SOCKET_ERROR)
  53.     {
  54.         printf("bind failed with error: %d\n", WSAGetLastError());
  55.         freeaddrinfo(result);
  56.         closesocket(ListenSocket);
  57.         WSACleanup();
  58.         return 1;
  59.     }
  60.  
  61.     freeaddrinfo(result);
  62.        
  63.     if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
  64.     {
  65.         printf("Listen failed with error: %ld\n", WSAGetLastError());
  66.         closesocket(ListenSocket);
  67.         WSACleanup();
  68.         return 1;
  69.     }
  70.  
  71.     // Handling pending connections
  72.  
  73.     puts("Waiting for a connection..");
  74.     SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
  75.     if (ClientSocket == INVALID_SOCKET)
  76.     {
  77.         printf("accept failed: %d\n", WSAGetLastError());
  78.         closesocket(ListenSocket);
  79.         WSACleanup();
  80.         return 1;
  81.     }
  82.  
  83.     #define DEFAULT_BUFLEN 512
  84.  
  85.     char recvbuf[DEFAULT_BUFLEN];
  86.     int iSendResult;
  87.     int recvbuflen = DEFAULT_BUFLEN;
  88.     int size =0 ;
  89.  
  90.     // Receive until the peer shuts down the connection
  91.     do
  92.     {
  93.         iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  94.         if (iResult > 0)
  95.         {
  96.             printf("Bytes received: %d\n", iResult);
  97.             size += iResult;
  98.  
  99.             // I thought this was the problem but after I removed it from the code it still was the same.
  100.             //
  101.             // Echo the buffer back to the sender
  102.             //iSendResult = send(ClientSocket, recvbuf, iResult, 0);
  103.             /*if (iSendResult == SOCKET_ERROR)
  104.             {
  105.                 printf("send failed: %d\n", WSAGetLastError());
  106.                 closesocket(ClientSocket);
  107.                 WSACleanup();
  108.                 return 1;
  109.             }*/
  110.             //printf("Bytes sent: %d\n", iSendResult);
  111.         }
  112.         else if (iResult == 0)
  113.             printf("Connection closing...\n");
  114.         else
  115.         {
  116.             printf("recv failed: %d\n", WSAGetLastError());
  117.             closesocket(ClientSocket);
  118.             WSACleanup();
  119.             return 1;
  120.         }
  121.  
  122.     }
  123.     while (iResult > 0);
  124.  
  125.     recvbuf[size] = 0;
  126.     printf("Data received: %s\n", recvbuf);
  127.  
  128.     // Stops responding to requests, still can accept tho
  129.     iResult = shutdown(ClientSocket, SD_SEND);
  130.     if (iResult == SOCKET_ERROR)
  131.     {
  132.         printf("shutdown failed: %d\n", WSAGetLastError());
  133.         closesocket(ClientSocket);
  134.         WSACleanup();
  135.         return 1;
  136.     }
  137.  
  138.     // Close socket, no receiving/sending
  139.     closesocket(ClientSocket);
  140.     WSACleanup();
  141. }
  142.  
RAW Paste Data