1- #include < print >
1+ #include < cstdio >
22#include < memory>
33#include < asio.hpp>
44
55using asio::ip::tcp;
6- using std::println;
76
8- const size_t port = 6969 ;
7+ const size_t port = 6970 ;
98asio::io_context io;
109asio::ip::tcp::acceptor server (io, tcp::endpoint(tcp::v4(), port));
1110
@@ -19,22 +18,32 @@ class Request {
1918 asio::streambuf buffer;
2019};
2120
22- void handle_request (const std::error_code& ec , tcp::socket socket) {
21+ void handle_request (const std::error_code& error , tcp::socket socket) {
2322 server.async_accept (io, &handle_request); // The Loop
2423
24+ if (error) {
25+ printf (" Error while accepting: %s\n " , error.message ().c_str ());
26+ return ;
27+ }
28+
2529 auto request = std::make_shared<Request>(std::move (socket));
2630
27- auto ep = request->socket .remote_endpoint ();
28- println (" Accepted connection {}" , ep.address ().to_string ());
31+ printf (" Accepted connection\n " );
2932
3033 asio::async_read_until (request->socket , request->buffer , ' \n ' ,
3134 [request](const auto &error, size_t bytes_transferred) {
32- auto ep = request->socket .remote_endpoint ();
33- println (" Received {} bytes from {}" , bytes_transferred, ep.address ().to_string ());
35+ if (error) {
36+ printf (" Error while reading: %s\n " , error.message ().c_str ());
37+ return ;
38+ }
39+ printf (" Received %zu bytes\n " , bytes_transferred);
3440 asio::async_write (request->socket , request->buffer ,
3541 [request](const auto &error, size_t bytes_transferred) {
36- auto ep = request->socket .remote_endpoint ();
37- println (" Sent {} bytes to {}" , bytes_transferred, ep.address ().to_string ());
42+ if (error) {
43+ printf (" Error while writing: %s\n " , error.message ().c_str ());
44+ return ;
45+ }
46+ printf (" Sent %zu bytes\n " , bytes_transferred);
3847 });
3948 });
4049}
@@ -43,14 +52,14 @@ int main()
4352{
4453 size_t seconds = 5 ;
4554 asio::steady_timer t (io, asio::chrono::seconds (seconds));
46- println (" Waiting for {} seconds" , seconds);
55+ printf (" Waiting for %zu seconds\n " , seconds);
4756 t.async_wait ([seconds](const std::error_code &e) {
48- println (" ------------------------------" );
49- println (" Done waiting {} seconds. Error: {} " , seconds, e.message ());
50- println (" ------------------------------" );
57+ printf (" ------------------------------\n " );
58+ printf (" Done waiting %zu seconds. Error: %s \n " , seconds, e.message (). c_str ());
59+ printf (" ------------------------------\n " );
5160 });
5261
53- println (" Listening too 127.0.0.1:{} " , port);
62+ printf (" Listening too 127.0.0.1:%zu \n " , port);
5463 server.async_accept (io, &handle_request);
5564 io.run ();
5665
0 commit comments