Zgragselus

Client

Dec 19th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #pragma comment(lib, "Ws2_32.lib")
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7.  
  8. #include "Command.h"
  9.  
  10. #pragma comment(lib, "Ws2_32.lib")
  11.  
  12. const char* SERVER_IP = "127.0.0.1"; // Loopback - server is local
  13. const int SERVER_PORT = 8080;
  14.  
  15. int main() {
  16.     WSADATA wsaData;
  17.     int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  18.     if (result != 0) {
  19.         std::cerr << "WSAStartup failed with error: " << result << std::endl;
  20.         return 1;
  21.     }
  22.  
  23.     SOCKET clientSocket = INVALID_SOCKET;
  24.     struct addrinfo* info = nullptr;
  25.     struct addrinfo 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.  
  32.     std::string portStr = std::to_string(SERVER_PORT);
  33.     result = getaddrinfo(SERVER_IP, portStr.c_str(), &hints, &info);
  34.     if (result != 0) {
  35.         std::cerr << "getaddrinfo failed with error: " << result << std::endl;
  36.         WSACleanup();
  37.         return 1;
  38.     }
  39.  
  40.     clientSocket = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
  41.     if (clientSocket == INVALID_SOCKET) {
  42.         std::cerr << "Error at socket(): " << WSAGetLastError() << std::endl;
  43.         freeaddrinfo(info);
  44.         WSACleanup();
  45.         return 1;
  46.     }
  47.  
  48.     result = connect(clientSocket, info->ai_addr, (int)info->ai_addrlen);
  49.     if (result == SOCKET_ERROR) {
  50.         std::cerr << "Failed to connect to server: " << WSAGetLastError() << std::endl;
  51.         closesocket(clientSocket);
  52.         freeaddrinfo(info);
  53.         WSACleanup();
  54.         return 1;
  55.     }
  56.  
  57.     // Cleanup resolved address info, no longer needed
  58.     freeaddrinfo(info);
  59.  
  60.     std::cout << "Successfully connected to server at " << SERVER_IP << ":" << SERVER_PORT << std::endl;
  61.     std::cout << "Start typing commands (or type 'quit'):" << std::endl;
  62.  
  63.     std::string command;
  64.     while (std::getline(std::cin, command) && command != "quit") {
  65.         if (command.empty()) continue;
  66.  
  67.         int bytesSent = 0;
  68.  
  69.         // Split command to tokens
  70.         std::vector<std::string> tokens = {};
  71.         size_t pos = 0;
  72.         while ((pos = command.find(' ')) != std::string::npos) {
  73.             tokens.push_back(command.substr(0, pos));
  74.             command.erase(0, pos + 1);
  75.         }
  76.         tokens.push_back(command);
  77.  
  78.         if (tokens[0] == "list")
  79.         {
  80.             bytesSent = send(clientSocket, "LST1", 4, 0);
  81.         }
  82.         else if (tokens[0] == "score")
  83.         {
  84.             bytesSent = send(clientSocket, "GET1", 4, 0);
  85.         }
  86.         else if (tokens[0] == "mode")
  87.         {
  88.             char message[5] = "MOD1";
  89.             if (tokens.size() > 1)
  90.             {
  91.                 message[4] = tokens[1][0];
  92.             }
  93.             else
  94.             {
  95.                 message[4] = '0'; // Default mode
  96.             }
  97.             bytesSent = send(clientSocket, message, 5, 0);
  98.         }
  99.         else if (tokens[0] == "select")
  100.         {
  101.             // Merge all tokens from 1 onward into a single name (in case it has spaces)
  102.             std::string name;
  103.             for (size_t i = 1; i < tokens.size(); i++)
  104.             {
  105.                 if (i > 1) name += " ";
  106.                 name += tokens[i];
  107.             }
  108.             name += ';';
  109.  
  110.             Command c(Command::Type::TYPE_SELECT_NAME, { Command::Arg(name) });
  111.             size_t outSize = 0;
  112.             char* serialized = c.Serialize(&outSize);
  113.             int bytesSent = send(clientSocket, serialized, outSize, 0);
  114.             delete[] serialized;
  115.         }
  116.         else if (tokens[0] == "getposition")
  117.         {
  118.             bytesSent = send(clientSocket, "POS1", 4, 0);
  119.         }
  120.         else if (tokens[0] == "translation")
  121.         {
  122.             Command c(Command::Type::TYPE_TRANSLATE, { Command::Arg(std::stof(tokens[1])), Command::Arg(std::stof(tokens[2])), Command::Arg(std::stof(tokens[3])) });
  123.             size_t outSize = 0;
  124.             char* serialized = c.Serialize(&outSize);
  125.             int bytesSent = send(clientSocket, serialized, outSize, 0);
  126.             delete[] serialized;
  127.         }
  128.         else
  129.         {
  130.             std::cout << "Unknown command." << std::endl;
  131.             continue;
  132.         }
  133.  
  134.         if (bytesSent == SOCKET_ERROR) {
  135.             std::cerr << "send failed: " << WSAGetLastError() << std::endl;
  136.             break;
  137.         }
  138.  
  139.         char recvbuf[1024];
  140.         int bytesRecv = recv(clientSocket, recvbuf, 1024, 0);
  141.  
  142.         if (bytesRecv > 0) {
  143.             recvbuf[bytesRecv] = '\0';
  144.             std::cout << "Server Response: " << recvbuf;
  145.             // C# server sends a newline, so no need to add one here.
  146.         }
  147.         else if (bytesRecv == 0) {
  148.             std::cout << "Connection closed by server." << std::endl;
  149.             break;
  150.         }
  151.         else {
  152.             std::cerr << "recv failed: " << WSAGetLastError() << std::endl;
  153.             break;
  154.         }
  155.     }
  156.  
  157.     closesocket(clientSocket);
  158.     WSACleanup();
  159.  
  160.     std::cout << "Client shut down." << std::endl;
  161.     return 0;
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment