-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
55 lines (45 loc) · 1.24 KB
/
Server.cpp
File metadata and controls
55 lines (45 loc) · 1.24 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
#include "Server.h"
#include <iostream>
#include <algorithm>
using namespace std;
Server::Server(const int port) {
if (!socket.create()) {
throw SocketException("Failed to create a socket");
} else if (!socket.bind(port)) {
throw SocketException("Couldn't bind socket to port");
} else if (!socket.listen()) {
throw SocketException("Couldn't listen to socket");
}
}
Server::~Server() {
if (socket.isValid()) {
cout << "Close server socket" << endl;
socket.close();
}
}
Server& Server::operator<<(const std::string &message) {
socket << message;
return *this;
}
Server& Server::operator>>(std::string &response) {
socket >> response;
return *this;
}
void Server::accept(Socket &socket) {
if (!this->socket.accept(socket)) {
throw SocketException("Server failed to accept connection on socket!");
}
connections.push_back(socket);
}
bool Server::removeConnection(Socket &socket) {
vector<Socket>::iterator index = std::find(connections.begin(), connections.end(), socket);
if (index != connections.end()) {
connections.erase(index);
return true;
} else {
return false;
}
}
int Server::getConnectionCount() {
return connections.size();
}