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