]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/echo/blocking_udp_echo_client.cpp
2 // blocking_udp_echo_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
14 #include <boost/asio.hpp>
16 using boost::asio::ip::udp
;
18 enum { max_length
= 1024 };
20 int main(int argc
, char* argv
[])
26 std::cerr
<< "Usage: blocking_udp_echo_client <host> <port>\n";
30 boost::asio::io_context io_context
;
32 udp::socket
s(io_context
, udp::endpoint(udp::v4(), 0));
34 udp::resolver
resolver(io_context
);
35 udp::resolver::results_type endpoints
=
36 resolver
.resolve(udp::v4(), argv
[1], argv
[2]);
38 using namespace std
; // For strlen.
39 std::cout
<< "Enter message: ";
40 char request
[max_length
];
41 std::cin
.getline(request
, max_length
);
42 size_t request_length
= strlen(request
);
43 s
.send_to(boost::asio::buffer(request
, request_length
), *endpoints
.begin());
45 char reply
[max_length
];
46 udp::endpoint sender_endpoint
;
47 size_t reply_length
= s
.receive_from(
48 boost::asio::buffer(reply
, max_length
), sender_endpoint
);
49 std::cout
<< "Reply is: ";
50 std::cout
.write(reply
, reply_length
);
53 catch (std::exception
& e
)
55 std::cerr
<< "Exception: " << e
.what() << "\n";