src/examples/cpp03/echo/async_udp_echo_server.cpp | src/examples/cpp11/echo/async_udp_echo_server.cpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·async_udp_echo_server.cpp | 2 | //·async_udp_echo_server.cpp |
3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~ | 3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // | 4 | // |
5 | //·Copyright·(c)·2003-2023·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2023·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) |
6 | // | 6 | // |
7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 7 | //·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> | |
14 | #include·"asio.hpp" | 13 | #include·"asio.hpp" |
15 | | 14 | |
16 | using·asio::ip::udp; | 15 | using·asio::ip::udp; |
17 | | 16 | |
18 | class·server | 17 | class·server |
19 | { | 18 | { |
20 | public: | 19 | public: |
21 | ··server(asio::io_context&·io_context,·short·port) | 20 | ··server(asio::io_context&·io_context,·short·port) |
22 | ····:·socket_(io_context,·udp::endpoint(udp::v4(),·port)) | 21 | ····:·socket_(io_context,·udp::endpoint(udp::v4(),·port)) |
23 | ··{ | 22 | ··{ |
24 | ····socket_.async_receive_from( | 23 | ····do_receive(); |
25 | ········asio::buffer(data_,·max_length),·sender_endpoint_, | |
26 | ········boost::bind(&server::handle_receive_from,·this, | |
27 | ··········asio::placeholders::error, | |
28 | ··········asio::placeholders::bytes_transferred)); | |
29 | ··} | 24 | ··} |
30 | | 25 | |
31 | ··void·handle_receive_from(const·asio::error_code&·error, | 26 | ··void·do_receive() |
32 | ······size_t·bytes_recvd) | |
33 | ··{ | 27 | ··{ |
34 | ····if·(!error·&&·bytes_recvd·>·0) | 28 | ····socket_.async_receive_from( |
35 | ····{ | 29 | ········asio::buffer(data_,·max_length),·sender_endpoint_, |
36 | ······socket_.async_send_to( | 30 | ········[this](std::error_code·ec,·std::size_t·bytes_recvd) |
37 | ··········asio::buffer(data_,·bytes_recvd),·sender_endpoint_, | 31 | ········{ |
38 | ··········boost::bind(&server::handle_send_to,·this, | 32 | ··········if·(!ec·&&·bytes_recvd·>·0) |
39 | ············asio::placeholders::error, | 33 | ··········{ |
40 | ············asio::placeholders::bytes_transferred)); | 34 | ············do_send(bytes_recvd); |
41 | ····} | 35 | ··········} |
42 | ····else | 36 | ··········else |
43 | ····{ | 37 | ··········{ |
44 | ······socket_.async_receive_from( | 38 | ············do_receive(); |
45 | ··········asio::buffer(data_,·max_length),·sender_endpoint_, | 39 | ··········} |
46 | ··········boost::bind(&server::handle_receive_from,·this, | 40 | ········}); |
47 | ············asio::placeholders::error, | |
48 | ············asio::placeholders::bytes_transferred)); | |
49 | ····} | |
50 | ··} | 41 | ··} |
51 | | 42 | |
52 | ··void·handle_send_to(const·asio::error_code&·/*error*/, | 43 | ··void·do_send(std::size_t·length) |
53 | ······size_t·/*bytes_sent*/) | |
54 | ··{ | 44 | ··{ |
55 | ····socket_.async_receive_from( | 45 | ····socket_.async_send_to( |
56 | ········asio::buffer(data_,·max_length),·sender_endpoint_, | 46 | ········asio::buffer(data_,·length),·sender_endpoint_, |
57 | ········boost::bind(&server::handle_receive_from,·this, | 47 | ········[this](std::error_code·/*ec*/,·std::size_t·/*bytes_sent*/) |
58 | ··········asio::placeholders::error, | 48 | ········{ |
59 | ··········asio::placeholders::bytes_transferred)); | 49 | ··········do_receive(); |
| 50 | ········}); |
60 | ··} | 51 | ··} |
61 | | 52 | |
62 | private: | 53 | private: |
63 | ··udp::socket·socket_; | 54 | ··udp::socket·socket_; |
64 | ··udp::endpoint·sender_endpoint_; | 55 | ··udp::endpoint·sender_endpoint_; |
65 | ··enum·{·max_length·=·1024·}; | 56 | ··enum·{·max_length·=·1024·}; |
66 | ··char·data_[max_length]; | 57 | ··char·data_[max_length]; |
67 | }; | 58 | }; |
68 | | 59 | |
69 | int·main(int·argc,·char*·argv[]) | 60 | int·main(int·argc,·char*·argv[]) |
70 | { | 61 | { |
71 | ··try | 62 | ··try |
72 | ··{ | 63 | ··{ |
73 | ····if·(argc·!=·2) | 64 | ····if·(argc·!=·2) |
74 | ····{ | 65 | ····{ |
75 | ······std::cerr·<<·"Usage:·async_udp_echo_server·<port>\n"; | 66 | ······std::cerr·<<·"Usage:·async_udp_echo_server·<port>\n"; |
76 | ······return·1; | 67 | ······return·1; |
77 | ····} | 68 | ····} |
78 | | 69 | |
79 | ····asio::io_context·io_context; | 70 | ····asio::io_context·io_context; |
80 | | 71 | |
81 | ····using·namespace·std;·//·For·atoi. | 72 | ····server·s(io_context,·std::atoi(argv[1])); |
82 | ····server·s(io_context,·atoi(argv[1])); | |
83 | | 73 | |
84 | ····io_context.run(); | 74 | ····io_context.run(); |
85 | ··} | 75 | ··} |
86 | ··catch·(std::exception&·e) | 76 | ··catch·(std::exception&·e) |
87 | ··{ | 77 | ··{ |
88 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 78 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; |
89 | ··} | 79 | ··} |
90 | | 80 | |
91 | ··return·0; | 81 | ··return·0; |
92 | } | 82 | } |