| 114 | //---------------------------------------------------------------------- |
| 115 | |
| 116 | int main(int argc, char* argv[]) |
| 117 | { |
| 118 | try |
| 119 | { |
| 120 | if (argc != 4) |
| 121 | { |
| 122 | std::cerr << "Usage: blocking_tcp_client <host> <port> <message>\n"; |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | boost::asio::io_context io_context; |
| 127 | |
| 128 | // Resolve the host name and service to a list of endpoints. |
| 129 | auto endpoints = tcp::resolver(io_context).resolve(argv[1], argv[2]); |
| 130 | |
| 131 | tcp_socket socket(io_context); |
| 132 | |
| 133 | // Run an asynchronous connect operation with a timeout. |
| 134 | boost::asio::async_connect(socket, endpoints, |
| 135 | close_after(std::chrono::seconds(10), socket)); |
| 136 | |
| 137 | auto time_sent = std::chrono::steady_clock::now(); |
| 138 | |
| 139 | // Run an asynchronous write operation with a timeout. |
| 140 | std::string msg = argv[3] + std::string("\n"); |
| 141 | boost::asio::async_write(socket, boost::asio::buffer(msg), |
| 142 | close_after(std::chrono::seconds(10), socket)); |
| 143 | |
| 144 | for (std::string input_buffer;;) |
| 145 | { |
| 146 | // Run an asynchronous read operation with a timeout. |
| 147 | std::size_t n = boost::asio::async_read_until(socket, |
| 148 | boost::asio::dynamic_buffer(input_buffer), '\n', |
| 149 | close_after(std::chrono::seconds(10), socket)); |
| 150 | |
| 151 | std::string line(input_buffer.substr(0, n - 1)); |
| 152 | input_buffer.erase(0, n); |
| 153 | |
| 154 | // Keep going until we get back the line that was sent. |
| 155 | if (line == argv[3]) |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | auto time_received = std::chrono::steady_clock::now(); |
| 160 | |
| 161 | std::cout << "Round trip time: "; |
| 162 | std::cout << std::chrono::duration_cast< |
| 163 | std::chrono::microseconds>( |
| 164 | time_received - time_sent).count(); |
| 165 | std::cout << " microseconds\n"; |
| 166 | } |
| 167 | catch (std::exception& e) |
| 168 | { |
| 169 | std::cerr << "Exception: " << e.what() << "\n"; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
nothing calls this directly
no test coverage detected