c++ boost asio timeout for blocking connect

Timeouts are the wrong place for synchronous methods, there’s a lengthy discussion in the asio ticket tracker.

I cannot switch to async mode of

I find this highly unlikely given the timeout requirement, post the rest of your code and explain why you cannot use asynchronous operations.

When this question was asked, I guess ASIO did not have any example on how to accomplish what the OP needed, that is to timeout a blocking operation such as a blocking socket operation. Now there exists examples to show you exactly how to do this. the example seems long, but that is because it is WELL commented. It shows how to use the ioservice in a ‘one shot’ kind of mode.

I think the example is a great solution. The other solutions here break portability and don’t take advantage of ioservice. if portability is not important and the ioservice seems like to much overhead –THEN– you should not be using ASIO. No matter what, you will have an ioservice created (almost all ASIO functionality depends on it, even sync sockets) so, take advantage of it.

ASIO example of timeout on blocking call

The ASIO documentation has been updated, so check it out for new examples on how to overcome some of the ‘gotchas’ ASIO use to have.

Read more here: Source link