blocking_tcp_client.cpp 6.1 KB

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