Advertisement
Danicron

sad Dan

Oct 3rd, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <Windows.h>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. #pragma comment(lib, "Ws2_32.lib")
  8.  
  9. constexpr int bufflen = 512;
  10.  
  11.  
  12. int main()
  13. {
  14.     SetConsoleOutputCP(CP_UTF8);
  15.  
  16.     WSAData wsaData;
  17.  
  18.     int recvbufflen = bufflen;
  19.  
  20.     int iResult;
  21.     char *sendbuf = nullptr;
  22.     char recvbuf[bufflen];
  23.  
  24.  
  25.  
  26.  
  27.     // Initialize Winsock
  28.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  29.     if (iResult != 0) {
  30.         std::cout << "WSAStartup failed: " << iResult << std::endl;
  31.         return 1;
  32.     }
  33.  
  34.     struct addrinfo *result = NULL,
  35.         *ptr = NULL,
  36.         hints;
  37.  
  38.     ZeroMemory(&hints, sizeof(hints));
  39.     hints.ai_family = AF_UNSPEC;
  40.     hints.ai_socktype = SOCK_STREAM;
  41.     hints.ai_protocol = IPPROTO_TCP;
  42.  
  43.     iResult = getaddrinfo("23.111.136.202", "4000", &hints, &result);
  44.     if (iResult != 0) {
  45.         printf("getaddrinfo failed: %d\n", iResult);
  46.         WSACleanup();
  47.         return 1;
  48.     }
  49.     else
  50.     {
  51.         std::cout << "Connection Successful!" << std::endl;
  52.     }
  53.  
  54.     SOCKET ConnectSocket = INVALID_SOCKET;
  55.  
  56.     ptr = result;
  57.  
  58.     // Create a SOCKET for connecting to server
  59.     ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  60.         ptr->ai_protocol);
  61.  
  62.     if (ConnectSocket == INVALID_SOCKET) {
  63.         std::cout << "Error at socket(): " << WSAGetLastError() << std::endl;
  64.         freeaddrinfo(result);
  65.         WSACleanup();
  66.         return 1;
  67.     }
  68.  
  69.     iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  70.     if (iResult == SOCKET_ERROR) {
  71.         closesocket(ConnectSocket);
  72.         ConnectSocket = INVALID_SOCKET;
  73.     }
  74.  
  75.     freeaddrinfo(result);
  76.  
  77.     if (ConnectSocket == INVALID_SOCKET) {
  78.         std::cout << "Unable to connect to server!" << std::endl;
  79.         WSACleanup();
  80.         return 1;
  81.     }
  82.  
  83.     do {
  84.         iResult = recv(ConnectSocket, recvbuf, recvbufflen, 0);
  85.         if (iResult > 0)
  86.         {
  87.             for (int i = 7; i < bufflen; i++)
  88.             {
  89.                 std::cout << recvbuf[i] << std::endl;
  90.             }
  91.             //std::cout << iResult << std::endl;
  92.         }
  93.         else if (iResult == 0)
  94.         {
  95.             return 2;  
  96.         }
  97.         else
  98.             printf("recv failed: %d\n", WSAGetLastError());
  99.     } while (iResult > 0);
  100.  
  101.     getchar();
  102.  
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement