excitoon
excitoon

Reputation: 450

async_connect blocks io_service::run_one() in boost::asio

I'm trying to run following code:

some_sock.async_connect(...); // handle_connect() sets the 'condition' flag

boost::asio::deadline_timer t(ios, boost::posix_time::seconds(2));
while (t.expires_from_now() >= boost::posix_time::seconds(0))
{
  ios.run_one();
  if (condition) return;
}

Desirable behavior is return from run_one() after timer t is expired (after 2 seconds). Actually, run_one() blocks until SYN-ACK or RST is received. If the server is not responding, run_one() will block for a much longer timeout than 2 seconds.

What should I do to wait specified amount of time for connect doing some work in background?

Thanks.

Upvotes: 3

Views: 2610

Answers (2)

Sam Miller
Sam Miller

Reputation: 24174

use io_service::run and deadline_timer::async_wait as described in the async tcp client example.

Upvotes: 4

Abyx
Abyx

Reputation: 12918

Call run_one() for connect() in another thread. On timeout call some_sock.cancel(). (Read its documentation first).

Upvotes: 0

Related Questions