-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerSocket.cpp
57 lines (46 loc) · 1.18 KB
/
ServerSocket.cpp
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
#include <iostream>
#include <string.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include "ServerSocket.h"
ServerSocket::ServerSocket(int fd, bool nagle_on) {
fd_ = fd;
is_initialized_ = true;
NagleOn(nagle_on);
}
bool ServerSocket::Init(int port) {
if (is_initialized_) {
return true;
}
struct sockaddr_in addr;
fd_ = socket(AF_INET, SOCK_STREAM, 0);
if (fd_ < 0) {
perror("ERROR: failed to create a socket");
return false;
}
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
if ((bind(fd_, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
perror("ERROR: failed to bind");
return false;
}
listen(fd_, 8);
is_initialized_ = true;
return true;
}
std::unique_ptr<ServerSocket> ServerSocket::Accept() {
int accepted_fd;
struct sockaddr_in addr;
unsigned int addr_size = sizeof(addr);
accepted_fd = accept(fd_, (struct sockaddr *) &addr, &addr_size);
if (accepted_fd < 0) {
perror("ERROR: failed to accept connection");
return nullptr;
}
return std::unique_ptr<ServerSocket>(new ServerSocket(accepted_fd, IsNagleOn()));
}