Advertisement
filip710

LV4 - Client

May 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <WS2tcpip.h>
  4. #include <sstream>
  5. #include <Windows.h>
  6.  
  7. #pragma comment(lib, "ws2_32.lib")
  8.  
  9. int main()
  10. {
  11.     std::string ipAddress = "127.0.0.1";            // IP Address of the server
  12.     int port = 54000;                       // Listening port # on the server
  13.  
  14.     WSAData data;
  15.     WORD ver = MAKEWORD(2, 2);
  16.     int wsResult = WSAStartup(ver, &data);
  17.     if (wsResult != 0)
  18.     {
  19.         std::cerr << "Can't start winsock, Error #" << wsResult << std::endl;
  20.         return -1;
  21.     }
  22.  
  23.     SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
  24.     if (sock == INVALID_SOCKET)
  25.     {
  26.         std::cerr << "Can't create socket, Error #" << WSAGetLastError() << std::endl;
  27.         WSACleanup();
  28.         return -2;
  29.     }
  30.  
  31.     sockaddr_in hint;
  32.     hint.sin_family = AF_INET;
  33.     hint.sin_port = htons(port);
  34.     inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
  35.  
  36.     int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
  37.     if (connResult == SOCKET_ERROR)
  38.     {
  39.         std::cerr << "Can't connect to server, Err #" << WSAGetLastError() << std::endl;
  40.         closesocket(sock);
  41.     }
  42.  
  43.     char buf[4096];
  44.  
  45.     std::cout << "Socket name: " << sock << std::endl;
  46.  
  47.     std::ostringstream socketNameStream;
  48.     socketNameStream << "N" << sock << "\r\n";
  49.     std::string socketName = socketNameStream.str();
  50.  
  51.     int sendResult = send(sock, socketName.c_str(), socketName.size() + 1, 0);
  52.     if (sendResult != SOCKET_ERROR)
  53.     {
  54.         ZeroMemory(buf, 4096);
  55.         int bytesreceived = recv(sock, buf, 4096, 0);
  56.         if (bytesreceived > 0)
  57.         {
  58.             std::cout << "SERVER> " << std::string(buf, 0, bytesreceived) << std::endl;
  59.         }
  60.     }
  61.  
  62.     while (true) {
  63.         ZeroMemory(buf, 4096);
  64.         int bytesReceived = recv(sock, buf, 4096, 0);
  65.         if (bytesReceived > 0)
  66.         {
  67.             std::cout << "SERVER> " << std::string(buf, 0, bytesReceived) << std::endl;
  68.  
  69.             if (buf[0] == 'P') {
  70.  
  71.                 std::string okMsg = "OK";
  72.                 send(sock, okMsg.c_str(), okMsg.size() + 1, 0);
  73.                 Sleep(100);
  74.                 closesocket(sock);
  75.                 WSACleanup();
  76.             }
  77.         }
  78.     }
  79.  
  80.     closesocket(sock);
  81.     WSACleanup();
  82.  
  83.     system("pause");
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement