Question

I have the following code:

...
namespace ba = boost::asio;
...
ba::ip::tcp::resolver resolver(ioService);
ba::ip::tcp::socket sock(ioService);

ba::ip::tcp::resolver::query query(the_ip, the_port);

ba::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

ba::ip::tcp::resolver::iterator end;

boost::system::error_code error = ba::error::host_not_found;

while (error && (endpoint_iterator != end))
{
    sock.close();

    sock.connect(*endpoint_iterator++, error);
}

When the server is up, it connects instantly and the rest of the code flows wonderfully, but when the server is offline or unreachable this while takes forever to exit.

Before the while: 09:04:37

When it exits: 09:05:40

Since I give query ONE IP and ONE PORT, shouldn't it exit instantly, given no connection can be established?

Are there any parameters I didn't configure? Something like "try X times" or "try for Y seconds"?

EDIT: As seen in the comments, the problem seems to be a search on the internet, but the connection is local! (192.168...)

Why does it search the internet for a local IP?

Was it helpful?

Solution

As @MartinJames said on the comments:

"The internet is quite big, and has all sorts of links, some with very high latency. Determining unreachability therefore takes a long time. That is what happens when you connect all the computers on a planet together."

I tried disconnecting from the internet and the while exited instantly.

But the IP I'm using is local (192.168...), so this is still strange.

Thanks, man.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top