MCPcopy Create free account
hub / github.com/boostorg/asio / main

Function main

example/cpp11/timeouts/blocking_token_tcp_client.cpp:116–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

114//----------------------------------------------------------------------
115
116int 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}

Callers

nothing calls this directly

Calls 10

close_afterClass · 0.85
stringFunction · 0.85
async_writeFunction · 0.85
bufferFunction · 0.85
async_read_untilFunction · 0.85
eraseMethod · 0.80
async_connectFunction · 0.50
nowFunction · 0.50
resolveMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected