-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
69 lines (55 loc) · 1.95 KB
/
Server.cpp
File metadata and controls
69 lines (55 loc) · 1.95 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
#include "Server.h"
#include "algorithm"
Server::Server(int port) throw (const char*) : t(nullptr){
//open a socket for the server
serverFD = socket(AF_INET, SOCK_STREAM, 0);
if (serverFD < 0)
throw "socket problem";
server = gethostbyname("localhost");
if(server==NULL)
throw "no such host";
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(port);
//bind the socket
if (bind(serverFD,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
throw "Failed to bind to port";
}
void Server::start(ClientHandler& ch) throw(const char*) {
t = new thread([this, &ch]() throw(const char*)->void {
int users = 0;
vector<thread*> ts;
//start listing
if (listen(serverFD, USERS) < 0)
throw "Failed to listen on socket";
auto addrlen = sizeof(serv_addr);
while(users < USERS) {
int connection = accept(serverFD, (struct sockaddr*)&serv_addr, (socklen_t*)&addrlen);
if (connection < 0)
throw "Failed to grab connection";
ts.push_back(new thread([connection, &ch](){ch.handle(connection);}));
++users;
}
for_each(ts.begin(), ts.end(),[](thread* thread)
{
if(thread->joinable()) {
thread->join();
}
delete(thread);
thread = nullptr;
}
);
ts.clear();
}
);
}
void Server::stop(){
t->join(); // do not delete this!
if (t != nullptr) {
delete(t);
t = nullptr;
}
}
Server::~Server() {
close(serverFD);
}