| 21 | using asio::ip::tcp; |
| 22 | |
| 23 | async_simple::coro::Lazy<void> start(asio::io_context &io_context, |
| 24 | std::string host, std::string port) { |
| 25 | asio::ip::tcp::socket socket(io_context); |
| 26 | auto ec = co_await async_connect(io_context, socket, host, port); |
| 27 | if (ec) { |
| 28 | std::cout << "Connect error: " << ec.message() << '\n'; |
| 29 | throw asio::system_error(ec); |
| 30 | } |
| 31 | std::cout << "Connect to " << host << ":" << port << " successfully.\n"; |
| 32 | const int max_length = 1024; |
| 33 | char write_buf[max_length] = {"hello async_simple"}; |
| 34 | char read_buf[max_length]; |
| 35 | const int count = 100000; |
| 36 | for (int i = 0; i < count; ++i) { |
| 37 | co_await async_write(socket, asio::buffer(write_buf, max_length)); |
| 38 | auto [error, reply_length] = co_await async_read_some( |
| 39 | socket, asio::buffer(read_buf, max_length)); |
| 40 | if (error == asio::error::eof) { |
| 41 | std::cout << "eof at message index: " << i << '\n'; |
| 42 | break; |
| 43 | } else if (error) { |
| 44 | std::cout << "error: " << error.message() |
| 45 | << ", message index: " << i << '\n'; |
| 46 | throw asio::system_error(error); |
| 47 | } |
| 48 | |
| 49 | // handle read data as your wish. |
| 50 | } |
| 51 | |
| 52 | std::cout << "Finished send and receive " << count |
| 53 | << " messages, client will close.\n"; |
| 54 | std::error_code ignore_ec; |
| 55 | socket.shutdown(asio::ip::tcp::socket::shutdown_both, ignore_ec); |
| 56 | io_context.stop(); |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char *argv[]) { |
| 60 | try { |
no test coverage detected