VladimirKostovsky

Osi_lab2_client

Dec 4th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>  // Для использования inet_pton
  3. #include <string>
  4. #include <iostream>
  5.  
  6. #pragma comment(lib, "Ws2_32.lib")
  7.  
  8. constexpr const int DEFAULT_BUF_SIZE = 512;
  9. constexpr const int DEFAULT_PORT = 2003;
  10.  
  11. int main(void)
  12. {
  13.     WSADATA wsaData;
  14.     std::string ip;
  15.     int iResult;
  16.     setlocale(LC_ALL, "RU");
  17.  
  18.     SOCKET ConnectSocket = INVALID_SOCKET;
  19.     struct sockaddr_in clientService;
  20.  
  21.     std::string sendbuf;
  22.     char recvbuf[DEFAULT_BUF_SIZE];
  23.  
  24.     // Init winSock
  25.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  26.     if (iResult != NO_ERROR)
  27.     {
  28.         std::cout << "WSAStartup failed: " << iResult << std::endl;
  29.         return 1;
  30.     }
  31.  
  32.     // Create Socket
  33.     ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  34.     if (ConnectSocket == SOCKET_ERROR)
  35.     {
  36.         std::cout << "socket error: " << WSAGetLastError() << std::endl;
  37.         WSACleanup();
  38.         return 1;
  39.     }
  40.  
  41.     // The sockaddr_in structure specifies the address family,
  42.     // IP address, and port of the server to be connected to.
  43.     std::cout << "Enter server IP: ";
  44.     std::cin >> ip;
  45.     std::cin.get();
  46.  
  47.     // Use inet_pton to convert IP address from string to binary
  48.     if (inet_pton(AF_INET, ip.c_str(), &(clientService.sin_addr)) <= 0)
  49.     {
  50.         std::cout << "Invalid IP address: " << ip << std::endl;
  51.         closesocket(ConnectSocket);
  52.         WSACleanup();
  53.         return 1;
  54.     }
  55.  
  56.     clientService.sin_family = AF_INET;
  57.     clientService.sin_port = htons(DEFAULT_PORT);
  58.  
  59.     // Connect to server.
  60.     iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
  61.     if (iResult == SOCKET_ERROR)
  62.     {
  63.         closesocket(ConnectSocket);
  64.         std::cout << "Unable to connect to server: " << WSAGetLastError() << std::endl;
  65.         WSACleanup();
  66.         return 1;
  67.     }
  68.  
  69.     // Send text strings to the server
  70.     std::cout << "Enter text lines to send to the server. Enter an empty line to finish:\n";
  71.     do
  72.     {
  73.         std::getline(std::cin, sendbuf);
  74.         if (!sendbuf.empty())
  75.         {
  76.             sendbuf += '\n'; // Add newline character as a delimiter
  77.             iResult = send(ConnectSocket, sendbuf.data(), static_cast<int>(sendbuf.length()), 0);
  78.             if (iResult == SOCKET_ERROR)
  79.             {
  80.                 std::cout << "send failed: " << WSAGetLastError() << std::endl;
  81.                 closesocket(ConnectSocket);
  82.                 WSACleanup();
  83.                 return 1;
  84.             }
  85.  
  86.             // Receive the modified text from the server
  87.             iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUF_SIZE, 0);
  88.             if (iResult > 0)
  89.             {
  90.                 recvbuf[iResult] = '\0';
  91.                 std::cout << "Modified text from server: " << recvbuf;
  92.             }
  93.             else if (iResult == 0)
  94.             {
  95.                 std::cout << "\nConnection closed by server\n";
  96.                 break;
  97.             }
  98.             else
  99.             {
  100.                 perror("recv");
  101.                 break;
  102.             }
  103.         }
  104.     } while (!sendbuf.empty());
  105.  
  106.     // Cleanup
  107.     closesocket(ConnectSocket);
  108.     WSACleanup();
  109.  
  110.     return 0;
  111. }
  112.  
Add Comment
Please, Sign In to add comment