src/​examples/​cpp03/​echo/​blocking_tcp_echo_ser​ver.​cppsrc/​examples/​cpp11/​echo/​blocking_tcp_echo_ser​ver.​cpp
1 /​/​1 /​/​
2 /​/​·​blocking_tcp_echo_ser​ver.​cpp2 /​/​·​blocking_tcp_echo_ser​ver.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying
8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 /​/​9 /​/​
10 10
11 #include·​<cstdlib>11 #include·​<cstdlib>
12 #include·​<iostream>12 #include·​<iostream>
13 #include·​<boost/​bind/​bind.​hpp>13 #include·​<thread>
14 #include·​<boost/​smart_ptr.​hpp>14 #include·​<utility>
15 #include·​"asio.​hpp"15 #include·​"asio.​hpp"
16 16
17 using·​asio:​:​ip:​:​tcp;​17 using·​asio:​:​ip:​:​tcp;​
18 18
19 const·​int·​max_length·​=·​1024;​19 const·​int·​max_length·​=·​1024;​
20 20
21 typedef·boost:​:​shared_ptr<tcp:​:​socket>·​socket_ptr;​21 void·​session(tcp:​:​socket·​sock)​
22
23 void·session(socket_ptr·sock)​
24 {22 {
25 ··​try23 ··​try
26 ··​{24 ··​{
27 ····​for·​(;​;​)​25 ····​for·​(;​;​)​
28 ····​{26 ····​{
29 ······​char·​data[max_length];​27 ······​char·​data[max_length];​
30 28
31 ······​asio:​:​error_code·​error;​29 ······​asio:​:​error_code·​error;​
32 ······​size_t·​length·​=·​sock-​>read_some(asio:​:​buffer(data)​,​·​error)​;​30 ······​size_t·​length·​=·​sock.​read_some(asio:​:​buffer(data)​,​·​error)​;​
33 ······​if·​(error·​==·​asio:​:​error:​:​eof)​31 ······​if·​(error·​==·​asio:​:​error:​:​eof)​
34 ········​break;​·​/​/​·​Connection·​closed·​cleanly·​by·​peer.​32 ········​break;​·​/​/​·​Connection·​closed·​cleanly·​by·​peer.​
35 ······​else·​if·​(error)​33 ······​else·​if·​(error)​
36 ········​throw·​asio:​:​system_error(error)​;​·​/​/​·​Some·​other·​error.​34 ········​throw·​asio:​:​system_error(error)​;​·​/​/​·​Some·​other·​error.​
37 35
38 ······​asio:​:​write(*sock,​·​asio:​:​buffer(data,​·​length)​)​;​36 ······​asio:​:​write(sock,​·​asio:​:​buffer(data,​·​length)​)​;​
39 ····​}37 ····​}
40 ··​}38 ··​}
41 ··​catch·​(std:​:​exception&·​e)​39 ··​catch·​(std:​:​exception&·​e)​
42 ··​{40 ··​{
43 ····​std:​:​cerr·​<<·​"Exception·​in·​thread:​·​"·​<<·​e.​what()​·​<<·​"\n";​41 ····​std:​:​cerr·​<<·​"Exception·​in·​thread:​·​"·​<<·​e.​what()​·​<<·​"\n";​
44 ··​}42 ··​}
45 }43 }
46 44
47 void·​server(asio:​:​io_context&·​io_context,​·​unsigned·​short·​port)​45 void·​server(asio:​:​io_context&·​io_context,​·​unsigned·​short·​port)​
48 {46 {
49 ··​tcp:​:​acceptor·​a(io_context,​·​tcp:​:​endpoint(tcp:​:​v4()​,​·​port)​)​;​47 ··​tcp:​:​acceptor·​a(io_context,​·​tcp:​:​endpoint(tcp:​:​v4()​,​·​port)​)​;​
50 ··​for·​(;​;​)​48 ··​for·​(;​;​)​
51 ··​{49 ··​{
52 ····​socket_ptr·sock(new·tcp:​:​socket(io_context)​)​;​50 ····​std:​:​thread(session,​·a.​accept()​)​.​detach()​;​
53 ····a.​accept(*sock)​;​
54 ····asio:​:​thread·t(boost:​:​bind(session,​·sock)​)​;​
55 ··​}51 ··​}
56 }52 }
57 53
58 int·​main(int·​argc,​·​char*·​argv[])​54 int·​main(int·​argc,​·​char*·​argv[])​
59 {55 {
60 ··​try56 ··​try
61 ··​{57 ··​{
62 ····​if·​(argc·​!=·​2)​58 ····​if·​(argc·​!=·​2)​
63 ····​{59 ····​{
64 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_tcp_echo_ser​ver·​<port>\n";​60 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_tcp_echo_ser​ver·​<port>\n";​
65 ······​return·​1;​61 ······​return·​1;​
66 ····​}62 ····​}
67 63
68 ····​asio:​:​io_context·​io_context;​64 ····​asio:​:​io_context·​io_context;​
69 65
70 ····using·namespace·​std;​·/​/​·For·atoi.​66 ····server(io_context,​·​std:​:​atoi(argv[1])​)​;​
71 ····server(io_context,​·atoi(argv[1])​)​;​
72 ··​}67 ··​}
73 ··​catch·​(std:​:​exception&·​e)​68 ··​catch·​(std:​:​exception&·​e)​
74 ··​{69 ··​{
75 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​70 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
76 ··​}71 ··​}
77 72
78 ··​return·​0;​73 ··​return·​0;​
79 }74 }