Advertisement
KonaJjr

SP_LV4_ZAD1_Klijent

Jan 14th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. // Client.cpp
  2. #include <winsock2.h>
  3. #include <ws2tcpip.h>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. #pragma comment(lib, "Ws2_32.lib")
  8.  
  9. #define SERVER_IP "127.0.0.1"
  10. #define PORT 8080
  11.  
  12. int main() {
  13.     WSADATA wsaData;
  14.     SOCKET clientSocket;
  15.     sockaddr_in serverAddr;
  16.     char buffer[1024];
  17.     std::string name;
  18.  
  19.     // Initialize WinSock
  20.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  21.         std::cerr << "WSAStartup failed.\n";
  22.         return 1;
  23.     }
  24.  
  25.     // Create socket
  26.     clientSocket = socket(AF_INET, SOCK_STREAM, 0);
  27.     if (clientSocket == INVALID_SOCKET) {
  28.         std::cerr << "Socket creation failed.\n";
  29.         WSACleanup();
  30.         return 1;
  31.     }
  32.  
  33.     // Set server address
  34.     serverAddr.sin_family = AF_INET;
  35.     serverAddr.sin_port = htons(PORT);
  36.     if (InetPtonA(AF_INET, SERVER_IP, &serverAddr.sin_addr) <= 0) {
  37.         std::cerr << "Invalid address or address not supported.\n";
  38.         closesocket(clientSocket);
  39.         WSACleanup();
  40.         return 1;
  41.     }
  42.  
  43.     // Connect to server
  44.     if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
  45.         std::cerr << "Connection failed.\n";
  46.         closesocket(clientSocket);
  47.         WSACleanup();
  48.         return 1;
  49.     }
  50.  
  51.     // Send name to server
  52.     std::cout << "Enter your name: ";
  53.     std::cin >> name;
  54.     std::string message = "N" + name;
  55.     send(clientSocket, message.c_str(), message.size(), 0);
  56.  
  57.     // Receive response from server
  58.     int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
  59.     if (bytesReceived > 0) {
  60.         buffer[bytesReceived] = '\0';
  61.         std::cout << "Server: " << buffer << "\n";
  62.     }
  63.  
  64.     // Wait for "P" message
  65.     bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
  66.     if (bytesReceived > 0) {
  67.         buffer[bytesReceived] = '\0';
  68.         if (std::string(buffer, bytesReceived) == "P") {
  69.             std::cout << "Server sent: P\n";
  70.  
  71.             // Prompt user to enter "OK"
  72.             std::string userInput;
  73.             std::cout << "Enter 'OK': ";
  74.             std::cin >> userInput;
  75.  
  76.             // Send "OK" back to server
  77.             if (userInput == "OK") {
  78.                 send(clientSocket, userInput.c_str(), userInput.size(), 0);
  79.             }
  80.             else {
  81.                 std::cerr << "Invalid input. Closing connection.\n";
  82.             }
  83.         }
  84.     }
  85.  
  86.     closesocket(clientSocket);
  87.     WSACleanup();
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement