C++ Boost ASIO: how to read/write with a timeout?

Posted by Stéphane on Stack Overflow See other posts from Stack Overflow or by Stéphane
Published on 2010-12-29T10:07:44Z Indexed on 2010/12/29 11:54 UTC
Read the original article Hit count: 184

Filed under:
|
|

From reading other Stackoverflow entries and the boost::asio documentation, I've confirmed that there is no synchronous asio read/write calls that also provide an easy-to-use timeout as a parameter to the call.

I'm in the middle of converting an old-school linux socket app with select(2) calls that employs timeouts, and I need to do more-or-less the same.

So what is the best way to do this in boost::asio? Looking at the asio documentation, there are many confusing examples of various things to do with timers, but I'm quite confused.

I'd love to see a simple-to-read example of this: Read from a socket, but wait for a maximum of X seconds after which the function either returns with nothing, or returns with whatever it was able to read from the socket before the timeout expired.

© Stack Overflow or respective owner

Related posts about c++

Related posts about sockets