-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cc
74 lines (62 loc) · 1.87 KB
/
server.cc
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
#include <iostream>
#include <list>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include "absl/container/flat_hash_map.h"
#include "connection.h"
#include "storage.h"
int main(int argc, char** argv) {
if (argc < 3) {
std::cerr << "Invalid arguments.\n"
<< "Arguments format: ./server [ip] [port]\n"
<< "Example: ./server 127.0.0.1 8701\n";
return 1;
}
const std::string server_ip = argv[1];
const uint16_t server_port = std::stoi(argv[2]);
absl::flat_hash_map<std::string, std::unique_ptr<Storage>> storage;
int socket_fd = socket(AF_INET, SOCK_STREAM, 6);
if (socket_fd == -1) {
perror("Error creating socket");
return 1;
}
const sockaddr_in socket_address = {
.sin_family = AF_INET,
.sin_port = htons(server_port),
.sin_addr = {inet_addr(server_ip.c_str())}};
if (bind(socket_fd, reinterpret_cast<const sockaddr*>(&socket_address),
sizeof(socket_address)) == -1) {
perror("Error binding socket");
if (close(socket_fd) == -1) {
perror("Error closing socket");
}
return 1;
}
if (listen(socket_fd, 5) == -1) {
perror("Error enabling socket to listen");
if (close(socket_fd) == -1) {
perror("Error closing socket");
}
return 1;
}
std::list<Connection> connections;
while (true) {
sockaddr_in client_socket_address{};
socklen_t sock_addr_size = sizeof(sockaddr_in);
int client_socket_fd = accept(
socket_fd,
reinterpret_cast<sockaddr*>(&client_socket_address),
&sock_addr_size);
if (client_socket_fd == -1) {
perror("Error accepting connection from client");
continue;
}
connections.emplace_back(&storage, client_socket_fd);
connections.remove_if([](const Connection& connection) {
return connection.IsClosed();
});
}
return 0;
}