Question Details

No question body available.

Tags

c++ boost-asio

Answers (1)

June 13, 2026 Score: 0 Rep: 401,465 Quality: Medium Completeness: 80%

You start out with a bold claim (cancellation can lose data). I don't think that's true.

Also, you can significantly simplify your code and also allow for concurrent connections at the same time. At the root of this, I detect a C-coding style, so allow me to show C++20 style:

Live On Compiler Explorer

#include 
#include 
#include 
namespace asio = boost::asio;
using namespace std::chronoliterals;
using asio::ip::tcp;
using boost::system::errorcode;

asio::awaitable Session(tcp::socket s) try { errorcode ec; auto timed = cancelafter(5s, asio::redirecterror(ec));

for (std::array data; !ec;) { auto nread = co
await asyncread(s, asio::buffer(data), timed);

if (ec == asio::error::operation
aborted) { std::cout