-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsockpuppet_tcp_server.cpp
83 lines (70 loc) · 2.37 KB
/
sockpuppet_tcp_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
#include "sockpuppet/socket.h" // for Acceptor
#include <cstdlib> // for EXIT_SUCCESS
#include <iostream> // for std::cout
#include <string> // for std::string
using namespace sockpuppet;
void HandleConnect(std::pair<SocketTcp, Address> p)
try {
auto &&clientSock = p.first;
auto &&clientAddr = p.second;
std::cerr << "connection "
<< to_string(clientAddr)
<< " <- "
<< to_string(clientSock.LocalAddress())
<< " accepted"
<< std::endl;
// receive and print until Ctrl-C or client disconnect
for(;;) {
// wait for and receive incoming data into provided buffer
// negative timeout -> blocking until receipt
char buffer[256];
constexpr Duration noTimeout(-1);
size_t received = *clientSock.Receive(buffer,
sizeof(buffer),
noTimeout);
// print whatever has just been received
std::cout << std::string(buffer, received) << std::flush;
}
} catch (std::exception const &e) {
// (most probably) client disconnected
std::cerr << e.what() << std::endl;
}
[[noreturn]] void Server(Address bindAddress)
{
// bind a TCP server socket to given address
// (you can turn this into a TLS-encrypted server
// by adding arguments for certificate and key file path)
Acceptor server(bindAddress);
// listen for and accept incoming connections until Ctrl-C
for(;;) {
// print the bound TCP socket address
// (might have OS-assigned interface and port number if
// it has not been explicitly set in the bind address)
std::cerr << "listening at "
<< to_string(server.LocalAddress())
<< std::endl;
// wait for and accept incoming connections
// negative timeout -> blocking until connection
constexpr Duration noTimeout(-1);
HandleConnect(*server.Listen(noTimeout));
}
}
int main(int argc, char *argv[])
try {
if(argc < 2) {
std::cerr << "Usage: " << argv[0]
<< " SOURCE\n\n"
"\tSOURCE is an address string to bind to, "
"e.g. \"localhost:8554\""
<< std::endl;
return EXIT_FAILURE;
}
// parse given address string
Address bindAddress(argv[1]);
// create and run a TCP server socket
Server(bindAddress);
return EXIT_SUCCESS;
} catch (std::exception const &e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}