-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerSocket.cpp
More file actions
151 lines (131 loc) · 4 KB
/
ServerSocket.cpp
File metadata and controls
151 lines (131 loc) · 4 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "ServerSocket.hpp"
ServerSocket::ServerSocket(const char *port) {
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int status = getaddrinfo(NULL, port, &hints, &address_info);
if (status != 0) {
throw std::runtime_error(gai_strerror(status));
}
socket_fd = socket(
address_info->ai_family,
address_info->ai_socktype,
address_info->ai_protocol
);
if (socket_fd < 0)
throw std::runtime_error("Socket not open");
}
ServerSocket::~ServerSocket() {
freeaddrinfo(address_info);
close(socket_fd);
for (int i = 0; i < client_sockets.size(); i++) {
close(client_sockets[i]);
}
}
int ServerSocket::bindSocket() {
//makes socket be able to bind to port if it had binded to it before
int yes = 1;
int status = setsockopt(
socket_fd,
SOL_SOCKET,
SO_REUSEADDR,
&yes,
sizeof(int)
);
if (status == -1) {
perror("setsockopt");
exit(1);
}
int socket_bind = bind(
socket_fd,
address_info->ai_addr,
address_info->ai_addrlen
);
if (socket_bind < 0)
throw std::runtime_error("Bind failed!");
return socket_bind;
}
int ServerSocket::listenSocket() {
int socket_listen = listen(socket_fd, BACK_LOG);
if (socket_listen < 0)
throw std::runtime_error("Listen failed!");
return socket_listen;
}
int ServerSocket::acceptSocket() {
struct sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof client_addr;
int client_socket_fd = accept(
socket_fd,
(struct sockaddr *) &client_addr,
&client_addr_size
);
if (client_socket_fd != -1)
client_sockets.push_back(client_socket_fd);
return client_socket_fd;
}
int ServerSocket::sendData(int client_fd, char *data, int data_size) {
int bytes_sent;
int total_size = data_size;
while (data_size) {
bytes_sent = send(client_fd, data, data_size, 0);
if (bytes_sent == -1) {
return -1;
}
data += bytes_sent;
data_size -= bytes_sent;
}
return total_size;
}
int ServerSocket::recvData(int client_fd, char *buffer, int buffer_size) {
int bytes_recv = recv(client_fd, buffer, buffer_size, 0);
if (bytes_recv == -1)
throw std::runtime_error("Receive message failed!");
else if (bytes_recv == 0) {
closeClientSocket(client_fd);
}
return bytes_recv;
}
int ServerSocket::closeClientSocket(int client_fd) {
close(client_fd);
for (int i = 0; i < client_sockets.size(); i++) {
if (client_sockets[i] == client_fd) {
client_sockets.erase(client_sockets.begin()+i);
break;
}
}
}
// int Socket::connectSocket(char *url, char* port) {
// struct addrinfo hints, *result;
// memset(&hints, 0, sizeof hints);
// hints.ai_family = AF_INET;
// hints.ai_socktype = SOCK_STREAM;
// int status = getaddrinfo(url, port, &hints, &result);
// if (status != 0) {
// throw std::runtime_error(gai_strerror(status));
// }
// int socket_connect = connect(
// socket_fd,
// result->ai_addr,
// result->ai_addrlen
// );
// if (socket_connect < 0)
// throw std::runtime_error("Connect failed!");
// return socket_connect;
// }
// char* Socket::getIP(char* url) {
// struct addrinfo hints, *result;
// memset(&hints, 0, sizeof hints);
// hints.ai_family = AF_INET;
// hints.ai_socktype = SOCK_STREAM;
// int status = getaddrinfo(url, NULL, &hints, &result);
// if (status != 0) {
// throw std::runtime_error(gai_strerror(status));
// }
// //assume first result will be the good one
// struct sockaddr_in *ip = (struct sockaddr_in*) result->ai_addr;
// char ip_str[INET_ADDRSTRLEN];
// inet_ntop(AF_INET, &(ip->sin_addr.s_addr), ip_str, sizeof ip_str);
// return ip_str;
// }