Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::asio failed to connect to localhost without WLAN

On my computer(Surface Pro 2), there is only one network adapter, it is a wireless LAN adapter.

I worked on a small C++ project, it uses boost::asio to connect to localhost and do its work, everything is just fine.

But today I found that if I disconnect the WLAN from the Internet, this program does not work.

An exception will be thrown by resolver of boost::asio :

tcp::resolver::query query("localhost", "10127");
tcp::resolver resolver(io_service_);
tcp::resolver::iterator iterator;

try {
    iterator = resolver.resolve(query);
}
catch (boost::system::system_error& e) {
    log(e.what());
}

And the error message was: the requested name is valid but no data of the requested type was found.

Ping to localhost is OK.

I feel puzzled, does a local network program need Internet ? Does a local network program need a LAN adapter ? Why ping works fine ?

like image 366
amanjiang Avatar asked Mar 11 '14 10:03

amanjiang


1 Answers

I just had the same problem on a linux machine and looked up the boost asio documentation. You just need to add a flag argument to the query constructor: tcp::resolver::query query("localhost","10127",tcp::resolver::query::canonical_name);

Note: the full scoped name of query is boost::asio::ip::tcp::resolver::query.

This happens because the default flags argument passed here is boost::asio::ip::tcp::resolver::query::address_configured, which means that the call should only resolve IPv4/IPv6 addresses if a non-loopback IPv4/IPv6 address is configured for the system.

like image 175
nradk Avatar answered Sep 21 '22 11:09

nradk