]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // blocking_udp_echo_client.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 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 <cstring> | |
13 | #include <iostream> | |
14 | #include <boost/asio.hpp> | |
15 | ||
16 | using boost::asio::ip::udp; | |
17 | ||
18 | enum { max_length = 1024 }; | |
19 | ||
20 | int main(int argc, char* argv[]) | |
21 | { | |
22 | try | |
23 | { | |
24 | if (argc != 3) | |
25 | { | |
26 | std::cerr << "Usage: blocking_udp_echo_client <host> <port>\n"; | |
27 | return 1; | |
28 | } | |
29 | ||
b32b8144 | 30 | boost::asio::io_context io_context; |
7c673cae | 31 | |
b32b8144 | 32 | udp::socket s(io_context, udp::endpoint(udp::v4(), 0)); |
7c673cae | 33 | |
b32b8144 FG |
34 | udp::resolver resolver(io_context); |
35 | udp::resolver::results_type endpoints = | |
36 | resolver.resolve(udp::v4(), argv[1], argv[2]); | |
7c673cae FG |
37 | |
38 | std::cout << "Enter message: "; | |
39 | char request[max_length]; | |
40 | std::cin.getline(request, max_length); | |
41 | size_t request_length = std::strlen(request); | |
b32b8144 | 42 | s.send_to(boost::asio::buffer(request, request_length), *endpoints.begin()); |
7c673cae FG |
43 | |
44 | char reply[max_length]; | |
45 | udp::endpoint sender_endpoint; | |
46 | size_t reply_length = s.receive_from( | |
47 | boost::asio::buffer(reply, max_length), sender_endpoint); | |
48 | std::cout << "Reply is: "; | |
49 | std::cout.write(reply, reply_length); | |
50 | std::cout << "\n"; | |
51 | } | |
52 | catch (std::exception& e) | |
53 | { | |
54 | std::cerr << "Exception: " << e.what() << "\n"; | |
55 | } | |
56 | ||
57 | return 0; | |
58 | } |