-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
96 lines (62 loc) · 1.35 KB
/
Copy pathServer.cpp
File metadata and controls
96 lines (62 loc) · 1.35 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "Server.h"
#include <iostream>
Server::Server()
{
serverSocket = {socket(AF_INET, SOCK_STREAM, 0)};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
idMap.clear();
bind(serverSocket, (sockaddr *)&serverAddress, sizeof(serverAddress));
listen(serverSocket, numberofClients);
}
Server::~Server()
{
close(serverSocket);
}
void Server::HandleConnection()
{
while(true)
{
int clientID {accept(serverSocket, nullptr, nullptr)};
clientIDs.insert(clientID);
std::thread(&Server::ConnectClient, this, clientID).detach();
}
}
void Server::ConnectClient(int clientID)
{
/*
char buffer[1024];
int bytes = read(clientID, buffer, 1023 * sizeof(char));
if(bytes < 1) return;
buffer[bytes] = 0;
std::string name {buffer};
{
std::lock_guard<std::mutex> lock(insertingMap);
idMap.insert({clientID, name});
}
*/
HandleCommunication(clientID);
}
void Server::HandleCommunication(int clientID)
{
std::cout<<"Working till here for clientID: "<<clientID<<std::endl;
char c;
read(clientID, &c, 1);
std::cout<<"Received: "<<c<<" from: "<<clientID<<std::endl;
/*
Message msg{};
while(true)
{
msg.clear();
msg.Read(clientID);
if(msg.End() == true)
{
clientIds.erase(clientID);
close(clientID);
break;
}
msg.Send();
}
*/
}