C ++ increase asio timeout to block connection

I have a C ++ boost client that blocks the connection and processes the message after receiving a response. I got a strange problem.

tcp::resolver::query query(tcp::v6(), this->host, port,tcp::resolver::query::v4_mapped); iterator = resolver.resolve(query); socket = new tcp::socket(io_service); socket->connect(*iterator); 

I tried to connect to a machine inaccessible to ping6 (but IPV6 was enabled). However, I did not get any errors when trying to resolve the request in line-2. As a result, it takes too much time when trying to connect before giving an error. My questions: -

1) Can a timeout block a connection from asio? I cannot switch to asynchronous mode of operation.

2) Why don't I get an error when it resolves an unreachable host?

Any advice would be very helpful.

+4
source share
2 answers

Timeouts are not the place for synchronous methods, there is a long discussion on the asio tracker ticket.

I cannot switch to asynchronous operation mode.

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

+1
source

When this question was asked, I believe that ASIO had no example on how to accomplish what the OP requires, that is, a break in the blocking operation, such as the blocking socket operation. Now there are examples to show you how to do this. the example seems long, but that is because it is well commented. It shows how to use ioservice in single snapshot mode.

I think this example is a great solution. Other solutions here violate portability and do not use ioservice. if portability is not important and the ioservice seems a lot overhead - ACTION - you should not use ASIO. Regardless, you will have created ioservice (almost all ASIO functionality depends on it, even synchronized sockets), so use it.

ASIO timeout example when blocking a call

The ASIO documentation has been updated, so check out the new examples on how to overcome some of the ASI "gotchas" features.

-1
source

All Articles