-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-thread.cpp
73 lines (63 loc) · 1.5 KB
/
multi-thread.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
#include <boost/asio.hpp>
#include <iostream>
#include <cstdlib>
namespace net = boost::asio;
using net::ip::tcp;
using net::dynamic_buffer;
using net::read_until;
void session(tcp::socket socket)
{
try
{
for (;;)
{
std::string command;
boost::system::error_code error;
auto length = read_until(socket, dynamic_buffer(command), '\n', error);
if (error == net::error::eof)
{
break;
}
else if (error)
{
throw boost::system::system_error(error);
}
if (command == "quit\n")
{
break;
}
net::write(socket, net::buffer(command, length));
}
}
catch (std::exception& e)
{
std::cerr << "Error in session: " << e.what() << '\n';
}
}
void server(net::io_context& io_context, unsigned short port)
{
tcp::acceptor acceptor {io_context, tcp::endpoint{tcp::v4(), port}};
for (;;)
{
auto socket = acceptor.accept();
std::thread { std::bind_front(&session, std::move(socket)) }.detach();
}
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Binding port must be specified\n";
return 1;
}
try
{
net::io_context io_context;
server(io_context, std::atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
}
return 0;
}