blocking_tcp_client.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // blocking_tcp_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/connect.hpp>
  12. #include <boost/asio/io_context.hpp>
  13. #include <boost/asio/ip/tcp.hpp>
  14. #include <boost/asio/read_until.hpp>
  15. #include <boost/system/system_error.hpp>
  16. #include <boost/asio/write.hpp>
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <string>
  20. using boost::asio::ip::tcp;
  21. //----------------------------------------------------------------------
  22. //
  23. // This class manages socket timeouts by running the io_context using the timed
  24. // io_context::run_for() member function. Each asynchronous operation is given
  25. // a timeout within which it must complete. The socket operations themselves
  26. // use lambdas as completion handlers. For a given socket operation, the client
  27. // object runs the io_context to block thread execution until the operation
  28. // completes or the timeout is reached. If the io_context::run_for() function
  29. // times out, the socket is closed and the outstanding asynchronous operation
  30. // is cancelled.
  31. //
  32. class client
  33. {
  34. public:
  35. void connect(const std::string& host, const std::string& service,
  36. std::chrono::steady_clock::duration timeout)
  37. {
  38. // Resolve the host name and service to a list of endpoints.
  39. auto endpoints = tcp::resolver(io_context_).resolve(host, service);
  40. // Start the asynchronous operation itself. The lambda that is used as a
  41. // callback will update the error variable when the operation completes.
  42. // The blocking_udp_client.cpp example shows how you can use std::bind
  43. // rather than a lambda.
  44. boost::system::error_code error;
  45. boost::asio::async_connect(socket_, endpoints,
  46. [&](const boost::system::error_code& result_error,
  47. const tcp::endpoint& /*result_endpoint*/)
  48. {
  49. error = result_error;
  50. });
  51. // Run the operation until it completes, or until the timeout.
  52. run(timeout);
  53. // Determine whether a connection was successfully established.
  54. if (error)
  55. throw std::system_error(error);
  56. }
  57. std::string read_line(std::chrono::steady_clock::duration timeout)
  58. {
  59. // Start the asynchronous operation. The lambda that is used as a callback
  60. // will update the error and n variables when the operation completes. The
  61. // blocking_udp_client.cpp example shows how you can use std::bind rather
  62. // than a lambda.
  63. boost::system::error_code error;
  64. std::size_t n = 0;
  65. boost::asio::async_read_until(socket_,
  66. boost::asio::dynamic_buffer(input_buffer_), '\n',
  67. [&](const boost::system::error_code& result_error,
  68. std::size_t result_n)
  69. {
  70. error = result_error;
  71. n = result_n;
  72. });
  73. // Run the operation until it completes, or until the timeout.
  74. run(timeout);
  75. // Determine whether the read completed successfully.
  76. if (error)
  77. throw std::system_error(error);
  78. std::string line(input_buffer_.substr(0, n - 1));
  79. input_buffer_.erase(0, n);
  80. return line;
  81. }
  82. void write_line(const std::string& line,
  83. std::chrono::steady_clock::duration timeout)
  84. {
  85. std::string data = line + "\n";
  86. // Start the asynchronous operation itself. The lambda that is used as a
  87. // callback will update the error variable when the operation completes.
  88. // The blocking_udp_client.cpp example shows how you can use std::bind
  89. // rather than a lambda.
  90. boost::system::error_code error;
  91. boost::asio::async_write(socket_, boost::asio::buffer(data),
  92. [&](const boost::system::error_code& result_error,
  93. std::size_t /*result_n*/)
  94. {
  95. error = result_error;
  96. });
  97. // Run the operation until it completes, or until the timeout.
  98. run(timeout);
  99. // Determine whether the read completed successfully.
  100. if (error)
  101. throw std::system_error(error);
  102. }
  103. private:
  104. void run(std::chrono::steady_clock::duration timeout)
  105. {
  106. // Restart the io_context, as it may have been left in the "stopped" state
  107. // by a previous operation.
  108. io_context_.restart();
  109. // Block until the asynchronous operation has completed, or timed out. If
  110. // the pending asynchronous operation is a composed operation, the deadline
  111. // applies to the entire operation, rather than individual operations on
  112. // the socket.
  113. io_context_.run_for(timeout);
  114. // If the asynchronous operation completed successfully then the io_context
  115. // would have been stopped due to running out of work. If it was not
  116. // stopped, then the io_context::run_for call must have timed out.
  117. if (!io_context_.stopped())
  118. {
  119. // Close the socket to cancel the outstanding asynchronous operation.
  120. socket_.close();
  121. // Run the io_context again until the operation completes.
  122. io_context_.run();
  123. }
  124. }
  125. boost::asio::io_context io_context_;
  126. tcp::socket socket_{io_context_};
  127. std::string input_buffer_;
  128. };
  129. //----------------------------------------------------------------------
  130. int main(int argc, char* argv[])
  131. {
  132. try
  133. {
  134. if (argc != 4)
  135. {
  136. std::cerr << "Usage: blocking_tcp_client <host> <port> <message>\n";
  137. return 1;
  138. }
  139. client c;
  140. c.connect(argv[1], argv[2], std::chrono::seconds(10));
  141. auto time_sent = std::chrono::steady_clock::now();
  142. c.write_line(argv[3], std::chrono::seconds(10));
  143. for (;;)
  144. {
  145. std::string line = c.read_line(std::chrono::seconds(10));
  146. // Keep going until we get back the line that was sent.
  147. if (line == argv[3])
  148. break;
  149. }
  150. auto time_received = std::chrono::steady_clock::now();
  151. std::cout << "Round trip time: ";
  152. std::cout << std::chrono::duration_cast<
  153. std::chrono::microseconds>(
  154. time_received - time_sent).count();
  155. std::cout << " microseconds\n";
  156. }
  157. catch (std::exception& e)
  158. {
  159. std::cerr << "Exception: " << e.what() << "\n";
  160. }
  161. return 0;
  162. }