-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketlib_server.h
More file actions
56 lines (49 loc) · 1.52 KB
/
socketlib_server.h
File metadata and controls
56 lines (49 loc) · 1.52 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
//
// Created by ferenci84 on 5/14/2020.
//
#ifndef SOCKETLIB2_SOCKETLIB_SERVER_H
#define SOCKETLIB2_SOCKETLIB_SERVER_H
#include "socketlib2.h"
#include "socketlib2_accept.h"
#include "socketlib2_receive.h"
#include "socketlib2_send.h"
SOCKET create_server(const string& addr, int port, int queue_size, int& err) {
SOCKET sock = open_socket(err);
if (sock == 0) {
cout << "Failed server socket() call, Error: " << err << endl;
return 0;
}
if (!bind(sock,(unsigned char*)addr.c_str(),port,err)) {
cout << "Failed to bind socket, Error: " << err << endl;
disconnect_socket_and_forget(sock);
return 0;
}
if (!listen(sock, queue_size,err)) {
cout << "Failed to listen socket, Error: " << err << endl;
disconnect_socket_and_forget(sock);
return 0;
}
cout << "Opened socket: " << sock << endl;
return sock;
}
class socket_server {
SOCKET sock;
bool connected;
accept_connections accept;
void open(const string& addr, int port, int queue_size, int& err) {
sock = create_server(addr,port,queue_size,err);
}
public:
socket_server(const string& addr, int port, int queue_size, int& err) : sock(0), connected(false) {
this->open(addr,port,queue_size,err);
if (sock != 0) {
connected = true;
accept = new accept_connections(sock);
}
}
~socket_server() {
disconnect_socket_and_forget(sock);
delete accept;
}
};
#endif //SOCKETLIB2_SOCKETLIB_SERVER_H