blocking_udp_client.cpp 4.3 KB

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