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