src/​examples/​cpp03/​echo/​blocking_udp_echo_ser​ver.​cppsrc/​examples/​cpp11/​echo/​blocking_udp_echo_ser​ver.​cpp
⋮⋮
1 /​/​1 /​/​
2 /​/​·​blocking_udp_echo_ser​ver.​cpp2 /​/​·​blocking_udp_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·​"asio.​hpp"13 #include·​"asio.​hpp"
14 14
15 using·​asio:​:​ip:​:​udp;​15 using·​asio:​:​ip:​:​udp;​
16 16
17 enum·​{·​max_length·​=·​1024·​};​17 enum·​{·​max_length·​=·​1024·​};​
18 18
19 void·​server(asio:​:​io_context&·​io_context,​·​unsigned·​short·​port)​19 void·​server(asio:​:​io_context&·​io_context,​·​unsigned·​short·​port)​
20 {20 {
21 ·​·​udp:​:​socket·​sock(io_context,​·​udp:​:​endpoint(udp:​:​v4()​,​·​port)​)​;​21 ·​·​udp:​:​socket·​sock(io_context,​·​udp:​:​endpoint(udp:​:​v4()​,​·​port)​)​;​
22 ·​·​for·​(;​;​)​22 ·​·​for·​(;​;​)​
23 ·​·​{23 ·​·​{
24 ·​·​·​·​char·​data[max_length];​24 ·​·​·​·​char·​data[max_length];​
25 ·​·​·​·​udp:​:​endpoint·​sender_endpoint;​25 ·​·​·​·​udp:​:​endpoint·​sender_endpoint;​
26 ·​·​·​·​size_t·​length·​=·​sock.​receive_from(26 ·​·​·​·​size_t·​length·​=·​sock.​receive_from(
27 ·​·​·​·​·​·​·​·​asio:​:​buffer(data,​·​max_length)​,​·​sender_endpoint)​;​27 ·​·​·​·​·​·​·​·​asio:​:​buffer(data,​·​max_length)​,​·​sender_endpoint)​;​
28 ·​·​·​·​sock.​send_to(asio:​:​buffer(data,​·​length)​,​·​sender_endpoint)​;​28 ·​·​·​·​sock.​send_to(asio:​:​buffer(data,​·​length)​,​·​sender_endpoint)​;​
29 ·​·​}29 ·​·​}
30 }30 }
31 31
32 int·​main(int·​argc,​·​char*·​argv[])​32 int·​main(int·​argc,​·​char*·​argv[])​
33 {33 {
34 ·​·​try34 ·​·​try
35 ·​·​{35 ·​·​{
36 ·​·​·​·​if·​(argc·​!=·​2)​36 ·​·​·​·​if·​(argc·​!=·​2)​
37 ·​·​·​·​{37 ·​·​·​·​{
38 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​blocking_udp_echo_ser​ver·​<port>\n";​38 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​blocking_udp_echo_ser​ver·​<port>\n";​
39 ·​·​·​·​·​·​return·​1;​39 ·​·​·​·​·​·​return·​1;​
40 ·​·​·​·​}40 ·​·​·​·​}
41 41
42 ·​·​·​·​asio:​:​io_context·​io_context;​42 ·​·​·​·​asio:​:​io_context·​io_context;​
43 43
44 ·​·​·​·​using·​namespace·​std;​·​/​/​·​For·​atoi.​44 ·​·​·​·​server(io_context,​·​std:​:​atoi(argv[1])​)​;​
45 ·​·​·​·​server(io_context,​·​atoi(argv[1])​)​;​
46 ·​·​}45 ·​·​}
47 ·​·​catch·​(std:​:​exception&·​e)​46 ·​·​catch·​(std:​:​exception&·​e)​
48 ·​·​{47 ·​·​{
49 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​48 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
50 ·​·​}49 ·​·​}
51 50
52 ·​·​return·​0;​51 ·​·​return·​0;​
53 }52 }