Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Server.cpp
- #include <winsock2.h>
- #include <iostream>
- #include <string>
- #include <vector>
- #pragma comment(lib, "Ws2_32.lib")
- #define PORT 8080
- #define MAX_CLIENTS 4
- int main() {
- WSADATA wsaData;
- SOCKET serverSocket, clientSocket;
- sockaddr_in serverAddr, clientAddr;
- int clientCount = 0;
- SOCKET clientSockets[MAX_CLIENTS];
- char buffer[1024];
- int clientAddrSize = sizeof(clientAddr);
- std::vector<std::string> clientNames;
- // Initialize WinSock
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
- std::cerr << "WSAStartup failed.\n";
- return 1;
- }
- // Create socket
- serverSocket = socket(AF_INET, SOCK_STREAM, 0);
- if (serverSocket == INVALID_SOCKET) {
- std::cerr << "Socket creation failed.\n";
- WSACleanup();
- return 1;
- }
- // Bind socket
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_addr.s_addr = INADDR_ANY;
- serverAddr.sin_port = htons(PORT);
- if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
- std::cerr << "Bind failed.\n";
- closesocket(serverSocket);
- WSACleanup();
- return 1;
- }
- // Listen for connections
- if (listen(serverSocket, MAX_CLIENTS) == SOCKET_ERROR) {
- std::cerr << "Listen failed.\n";
- closesocket(serverSocket);
- WSACleanup();
- return 1;
- }
- std::cout << "Server listening on port " << PORT << "...\n";
- while (clientCount < MAX_CLIENTS) {
- clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrSize);
- if (clientSocket == INVALID_SOCKET) {
- std::cerr << "Accept failed.\n";
- continue;
- }
- clientSockets[clientCount] = clientSocket;
- clientCount++;
- int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
- if (bytesReceived > 0) {
- buffer[bytesReceived] = '\0';
- std::string receivedMessage(buffer);
- // Provjera tipa poruke i izdvajanje imena
- if (receivedMessage[0] == 'N') {
- std::string clientName = receivedMessage.substr(1); // Sve nakon 'N' je ime
- clientNames.push_back(clientName);
- std::cout << "Client " << clientCount << " connected with name: " << clientName << "\n";
- // Pošalji broj klijenata natrag
- std::string response = "B" + std::to_string(clientCount);
- send(clientSocket, response.c_str(), response.size(), 0);
- }
- // Ako je četvrti klijent spojen, pošalji svim klijentima "P"
- if (clientCount == MAX_CLIENTS) {
- for (int i = 0; i < MAX_CLIENTS; i++) {
- send(clientSockets[i], "P", 1, 0);
- }
- // Čekaj "OK" od svih klijenata i zatvori njihove veze
- for (int i = 0; i < MAX_CLIENTS; i++) {
- int bytesReceived = recv(clientSockets[i], buffer, sizeof(buffer), 0);
- if (bytesReceived > 0) {
- buffer[bytesReceived] = '\0';
- if (std::string(buffer, bytesReceived) == "OK") {
- std::cout << "Client " << i + 1 << " acknowledged with OK.\n";
- }
- }
- closesocket(clientSockets[i]);
- }
- break;
- }
- }
- }
- closesocket(serverSocket);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement