Advertisement
KonaJjr

SP_LV4_ZAD1_Server

Jan 14th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. // Server.cpp
  2. #include <winsock2.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. #pragma comment(lib, "Ws2_32.lib")
  8.  
  9. #define PORT 8080
  10. #define MAX_CLIENTS 4
  11.  
  12. int main() {
  13.     WSADATA wsaData;
  14.     SOCKET serverSocket, clientSocket;
  15.     sockaddr_in serverAddr, clientAddr;
  16.     int clientCount = 0;
  17.     SOCKET clientSockets[MAX_CLIENTS];
  18.     char buffer[1024];
  19.     int clientAddrSize = sizeof(clientAddr);
  20.     std::vector<std::string> clientNames;
  21.  
  22.     // Initialize WinSock
  23.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  24.         std::cerr << "WSAStartup failed.\n";
  25.         return 1;
  26.     }
  27.  
  28.     // Create socket
  29.     serverSocket = socket(AF_INET, SOCK_STREAM, 0);
  30.     if (serverSocket == INVALID_SOCKET) {
  31.         std::cerr << "Socket creation failed.\n";
  32.         WSACleanup();
  33.         return 1;
  34.     }
  35.  
  36.     // Bind socket
  37.     serverAddr.sin_family = AF_INET;
  38.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  39.     serverAddr.sin_port = htons(PORT);
  40.  
  41.     if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
  42.         std::cerr << "Bind failed.\n";
  43.         closesocket(serverSocket);
  44.         WSACleanup();
  45.         return 1;
  46.     }
  47.  
  48.     // Listen for connections
  49.     if (listen(serverSocket, MAX_CLIENTS) == SOCKET_ERROR) {
  50.         std::cerr << "Listen failed.\n";
  51.         closesocket(serverSocket);
  52.         WSACleanup();
  53.         return 1;
  54.     }
  55.  
  56.     std::cout << "Server listening on port " << PORT << "...\n";
  57.  
  58.     while (clientCount < MAX_CLIENTS) {
  59.         clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrSize);
  60.         if (clientSocket == INVALID_SOCKET) {
  61.             std::cerr << "Accept failed.\n";
  62.             continue;
  63.         }
  64.  
  65.         clientSockets[clientCount] = clientSocket;
  66.         clientCount++;
  67.  
  68.         int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
  69.         if (bytesReceived > 0) {
  70.             buffer[bytesReceived] = '\0';
  71.             std::string receivedMessage(buffer);
  72.  
  73.             // Provjera tipa poruke i izdvajanje imena
  74.             if (receivedMessage[0] == 'N') {
  75.                 std::string clientName = receivedMessage.substr(1); // Sve nakon 'N' je ime
  76.                 clientNames.push_back(clientName);
  77.  
  78.                 std::cout << "Client " << clientCount << " connected with name: " << clientName << "\n";
  79.  
  80.                 // Pošalji broj klijenata natrag
  81.                 std::string response = "B" + std::to_string(clientCount);
  82.                 send(clientSocket, response.c_str(), response.size(), 0);
  83.             }
  84.  
  85.             // Ako je četvrti klijent spojen, pošalji svim klijentima "P"
  86.             if (clientCount == MAX_CLIENTS) {
  87.                 for (int i = 0; i < MAX_CLIENTS; i++) {
  88.                     send(clientSockets[i], "P", 1, 0);
  89.                 }
  90.  
  91.                 // Čekaj "OK" od svih klijenata i zatvori njihove veze
  92.                 for (int i = 0; i < MAX_CLIENTS; i++) {
  93.                     int bytesReceived = recv(clientSockets[i], buffer, sizeof(buffer), 0);
  94.                     if (bytesReceived > 0) {
  95.                         buffer[bytesReceived] = '\0';
  96.                         if (std::string(buffer, bytesReceived) == "OK") {
  97.                             std::cout << "Client " << i + 1 << " acknowledged with OK.\n";
  98.                         }
  99.                     }
  100.                     closesocket(clientSockets[i]);
  101.                 }
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107.     closesocket(serverSocket);
  108.     WSACleanup();
  109.     return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement