blocking_udp_client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // blocking_udp_client.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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. #include <boost/asio/buffer.hpp>
  11. #include <boost/asio/io_context.hpp>
  12. #include <boost/asio/ip/udp.hpp>
  13. #include <cstdlib>
  14. #include <boost/bind.hpp>
  15. #include <iostream>
  16. using boost::asio::ip::udp;
  17. //----------------------------------------------------------------------
  18. //
  19. // This class manages socket timeouts by running the io_context using the timed
  20. // io_context::run_for() member function. Each asynchronous operation is given
  21. // a timeout within which it must complete. The socket operations themselves
  22. // use boost::bind to specify the completion handler:
  23. //
  24. // +---------------+
  25. // | |
  26. // | receive |
  27. // | |
  28. // +---------------+
  29. // |
  30. // async_- | +----------------+
  31. // receive() | | |
  32. // +--->| handle_receive |
  33. // | |
  34. // +----------------+
  35. //
  36. // For a given socket operation, the client object runs the io_context to block
  37. // thread execution until the operation completes or the timeout is reached. If
  38. // the io_context::run_for() function times out, the socket is closed and the
  39. // outstanding asynchronous operation is cancelled.
  40. //
  41. class client
  42. {
  43. public:
  44. client(const udp::endpoint& listen_endpoint)
  45. : socket_(io_context_, listen_endpoint)
  46. {
  47. }
  48. std::size_t receive(const boost::asio::mutable_buffer& buffer,
  49. boost::asio::chrono::steady_clock::duration timeout,
  50. boost::system::error_code& ec)
  51. {
  52. // Start the asynchronous operation. The handle_receive function used as a
  53. // callback will update the ec and length variables.
  54. std::size_t length = 0;
  55. socket_.async_receive(boost::asio::buffer(buffer),
  56. boost::bind(&client::handle_receive, _1, _2, &ec, &length));
  57. // Run the operation until it completes, or until the timeout.
  58. run(timeout);
  59. return length;
  60. }
  61. private:
  62. void run(boost::asio::chrono::steady_clock::duration timeout)
  63. {
  64. // Restart the io_context, as it may have been left in the "stopped" state
  65. // by a previous operation.
  66. io_context_.restart();
  67. // Block until the asynchronous operation has completed, or timed out. If
  68. // the pending asynchronous operation is a composed operation, the deadline
  69. // applies to the entire operation, rather than individual operations on
  70. // the socket.
  71. io_context_.run_for(timeout);
  72. // If the asynchronous operation completed successfully then the io_context
  73. // would have been stopped due to running out of work. If it was not
  74. // stopped, then the io_context::run_for call must have timed out.
  75. if (!io_context_.stopped())
  76. {
  77. // Cancel the outstanding asynchronous operation.
  78. socket_.cancel();
  79. // Run the io_context again until the operation completes.
  80. io_context_.run();
  81. }
  82. }
  83. static void handle_receive(
  84. const boost::system::error_code& ec, std::size_t length,
  85. boost::system::error_code* out_ec, std::size_t* out_length)
  86. {
  87. *out_ec = ec;
  88. *out_length = length;
  89. }
  90. private:
  91. boost::asio::io_context io_context_;
  92. udp::socket socket_;
  93. };
  94. //----------------------------------------------------------------------
  95. int main(int argc, char* argv[])
  96. {
  97. try
  98. {
  99. using namespace std; // For atoi.
  100. if (argc != 3)
  101. {
  102. std::cerr << "Usage: blocking_udp_client <listen_addr> <listen_port>\n";
  103. return 1;
  104. }
  105. udp::endpoint listen_endpoint(
  106. boost::asio::ip::make_address(argv[1]),
  107. std::atoi(argv[2]));
  108. client c(listen_endpoint);
  109. for (;;)
  110. {
  111. char data[1024];
  112. boost::system::error_code ec;
  113. std::size_t n = c.receive(boost::asio::buffer(data),
  114. boost::asio::chrono::seconds(10), ec);
  115. if (ec)
  116. {
  117. std::cout << "Receive error: " << ec.message() << "\n";
  118. }
  119. else
  120. {
  121. std::cout << "Received: ";
  122. std::cout.write(data, n);
  123. std::cout << "\n";
  124. }
  125. }
  126. }
  127. catch (std::exception& e)
  128. {
  129. std::cerr << "Exception: " << e.what() << "\n";
  130. }
  131. return 0;
  132. }