-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatServer.hpp
More file actions
44 lines (39 loc) · 870 Bytes
/
ChatServer.hpp
File metadata and controls
44 lines (39 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// ChatServer.hpp
#pragma once
#include <vector>
#include <string>
#include <thread>
#include <mutex>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
typedef SOCKET socket_t;
#define SOCKET_ERROR_VAL INVALID_SOCKET
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
typedef int socket_t;
#define SOCKET_ERROR_VAL -1
#endif
class ChatServer
{
private:
socket_t serverSocket;
std::vector<socket_t> clientSockets;
std::vector<std::string> chatHistory;
std::mutex clientsMutex;
bool running;
void handleClient(socket_t clientSocket);
void broadcastMessage(const std::string &message, socket_t sender);
#ifdef _WIN32
static bool initializeWinsock();
#endif
public:
ChatServer(int port);
~ChatServer();
void start();
void stop();
};