-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
100 lines (84 loc) · 2.4 KB
/
server.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
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
// Standard C++ headers
#include <iostream>
#include <cstring>
#include <cstdlib>
// Socket headers
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
// UNIX system headers
#include <errno.h>
using namespace std;
const int SERVER_PORT = 5432;
const int MAX_PENDING = 5;
const int MAX_LINE = 256;
void handle_error(int eno, char const *msg)
{
if (eno == 0)
cerr << msg << endl;
else
cerr << msg << ": " << strerror(eno) << endl;
exit(errno);
}
bool request_dll_and_permission()
{
#if defined(WIN32)
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
return false;
}
return true;
#endif
}
int main(int argc, char *argv[])
{
bool granted = request_dll_and_permission();
if (!granted)
handle_error(errno, "simplex_server - socket not permited!");
int nread;
socklen_t len;
sockaddr_in sin;
sockaddr *sin_p = (sockaddr *)&sin;
sin.sin_family = AF_INET; // IP version 4
sin.sin_port = htons(SERVER_PORT);
sin.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all IP addresses
// Create socket
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1)
handle_error(errno, "simplex_server - socket");
// Bind socket to local address
int rstat = bind(s, sin_p, sizeof(sin));
if (rstat == -1)
handle_error(errno, "simplex_server - bind");
rstat = listen(s, MAX_PENDING);
if (rstat == -1)
handle_error(errno, "simplex_server - listen");
// Allocate a memory buffer for received messages
char *buf = new char[MAX_LINE];
// Wait for connections
while (true)
{
// Upon return the sin structure will contain the address of the
// connecting socket. Note that a new socket is returned.
len = sizeof(sin);
int new_s = accept(s, sin_p, &len);
if (new_s == -1)
handle_error(errno, "simplex_server - accept");
// Receive and print messages as long as client is connected.
while (true)
{
nread = recv(new_s, buf, MAX_LINE, 0);
if (nread == -1)
handle_error(0, "simplex_server - recv");
if (nread == 0)
break; // client has disconnected
cout << buf << flush;
}
closesocket(new_s);
}
closesocket(s);
delete[] buf;
return 0;
}