-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
54 lines (44 loc) · 1.27 KB
/
Copy pathServer.cpp
File metadata and controls
54 lines (44 loc) · 1.27 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
//Name: Noam Tzuberi ID:313374837
//Name: Orel Mishan ID:316551092
#include <unistd.h>
#include <csignal>
#include "Server.h"
Server::Server(int port) throw(const char *) {
fileDis = socket(AF_INET, SOCK_STREAM, 0);
if (fileDis < 0) {
throw "can't open socket";
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
//int checkBind = bind(fileDis, (struct sockaddr *) &server, sizeof server);
if (bind(fileDis, (struct sockaddr *) &server, sizeof server) < 0) {
throw "can't bind";
}
int checkListen = listen(fileDis, 3);
if (checkListen < 0)
throw "can't listen";
}
void sigHandler(int sigNum) {
}
void Server::start(ClientHandler &ch) throw(const char *) {
t = new std::thread([&ch, this]() {
//add time out to accept
while (!isStop) {
socklen_t clintSize = sizeof client;
int clientNum = accept(fileDis, (struct sockaddr *) &client, &clintSize);
if (clientNum > 0) {
ch.handle(clientNum);
close(clientNum);
}
}
close(fileDis);
});
}
void Server::stop() {
isStop = true;
t->join(); // do not delete this!
}
Server::~Server() {
delete t;
}