xXTurner

client WInSocks2

Aug 22nd, 2021 (edited)
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  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("109.xxx.xxx.xxx", "1337", &hints, &result);
  35.     if (iResult != 0)
  36.     {
  37.         printf("getaddrinfo failed: %d\n", iResult);
  38.         WSACleanup();
  39.         return 1;
  40.     }
  41.  
  42.     SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  43.  
  44.     if (ConnectSocket == INVALID_SOCKET)
  45.     {
  46.         printf("Error at socket(): %ld\n", WSAGetLastError());
  47.         freeaddrinfo(result);
  48.         WSACleanup();
  49.     }
  50.  
  51.     iResult = connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
  52.     if (iResult == SOCKET_ERROR)   
  53.     {
  54.         closesocket(ConnectSocket);
  55.         ConnectSocket = INVALID_SOCKET;
  56.     }
  57.  
  58.     // Should really try the next address returned by getaddrinfo
  59.     // if the connect call failed
  60.     // But for this simple example we just free the resources
  61.     // returned by getaddrinfo and print an error message
  62.  
  63.     freeaddrinfo(result);
  64.  
  65.     if (ConnectSocket == INVALID_SOCKET)
  66.     {
  67.         printf("Unable to connect to server!\n");
  68.         WSACleanup();
  69.         return 1;
  70.     }
  71.  
  72.  
  73.  
  74.     #define DEFAULT_BUFLEN 512
  75.  
  76.     int recvbuflen = DEFAULT_BUFLEN;
  77.  
  78.     const char* sendbuf = "this is a test";
  79.     char recvbuf[DEFAULT_BUFLEN];
  80.  
  81.     // Send an initial buffer
  82.     iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
  83.     if (iResult == SOCKET_ERROR)
  84.     {
  85.         printf("send failed: %d\n", WSAGetLastError());
  86.         closesocket(ConnectSocket);
  87.         WSACleanup();
  88.         return 1;
  89.     }
  90.  
  91.     printf("Bytes Sent: %ld\n", iResult);
  92.  
  93.     // shutdown the connection for sending since no more data will be sent
  94.     // the client can still use the ConnectSocket for receiving data
  95.     iResult = shutdown(ConnectSocket, SD_SEND);
  96.     if (iResult == SOCKET_ERROR)
  97.     {
  98.         printf("shutdown failed: %d\n", WSAGetLastError());
  99.         closesocket(ConnectSocket);
  100.         WSACleanup();
  101.         return 1;
  102.     }
  103.  
  104.     // Receive data until the server closes the connection
  105.     do
  106.     {
  107.         iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  108.         if (iResult > 0)
  109.             printf("Bytes received: %d\n", iResult);
  110.         else if (iResult == 0)
  111.             printf("Connection closed\n");
  112.         else
  113.             printf("recv failed: %d\n", WSAGetLastError());
  114.     }
  115.     while (iResult > 0);
  116.  
  117.     closesocket(ConnectSocket);
  118.     WSACleanup();
  119. }
  120.  
Add Comment
Please, Sign In to add comment