MCPcopy Create free account
hub / github.com/boostorg/asio / get_daytime

Function get_daytime

example/cpp11/futures/daytime_client.cpp:21–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19using boost::asio::ip::udp;
20
21void get_daytime(boost::asio::io_context& io_context, const char* hostname)
22{
23 try
24 {
25 udp::resolver resolver(io_context);
26
27 std::future<udp::resolver::results_type> endpoints =
28 resolver.async_resolve(
29 udp::v4(), hostname, "daytime",
30 boost::asio::use_future);
31
32 // The async_resolve operation above returns the endpoints as a future
33 // value that is not retrieved ...
34
35 udp::socket socket(io_context, udp::v4());
36
37 std::array<char, 1> send_buf = {{ 0 }};
38 std::future<std::size_t> send_length =
39 socket.async_send_to(boost::asio::buffer(send_buf),
40 *endpoints.get().begin(), // ... until here. This call may block.
41 boost::asio::use_future);
42
43 // Do other things here while the send completes.
44
45 send_length.get(); // Blocks until the send is complete. Throws any errors.
46
47 std::array<char, 128> recv_buf;
48 udp::endpoint sender_endpoint;
49 std::future<std::size_t> recv_length =
50 socket.async_receive_from(
51 boost::asio::buffer(recv_buf),
52 sender_endpoint,
53 boost::asio::use_future);
54
55 // Do other things here while the receive completes.
56
57 std::cout.write(
58 recv_buf.data(),
59 recv_length.get()); // Blocks until receive is complete.
60 }
61 catch (boost::system::system_error& e)
62 {
63 std::cerr << e.what() << std::endl;
64 }
65}
66
67int main(int argc, char* argv[])
68{

Callers 1

mainFunction · 0.85

Calls 8

bufferFunction · 0.85
async_resolveMethod · 0.45
async_send_toMethod · 0.45
beginMethod · 0.45
getMethod · 0.45
async_receive_fromMethod · 0.45
writeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected