Reputation: 1283
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 ?
Upvotes: 9
Views: 5273
Reputation: 700
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.
Upvotes: 10
Reputation: 8127
local network does not required internet to work. In your case, I don't know exact the problem of Windows TCP/IP layer with resolve localhost
when it does not have any DNS server config(Network card is not connected, ..etc). But you can use tcp::resolver::query query("127.0.0.1", "10127");
instead of tcp::resolver::query query("localhost", "10127");
which works always.
Upvotes: 0
Reputation: 5346
I have no explanation of why you have this error. However, what I did in a project was not by specifying the port number directly, but rather by constructing the endpoint instance in two steps. I don't recall the rationale at the time for doing it this way, but it might help you.
My proposed solution is something like this:
ip::tcp::resolver::query query(ip::tcp::v4(), "localhost", ""); // empty service name
tcp::resolver::iterator it_endpoint = resolver.resolve(query);
ip::tcp::endpoint endpoint(ip::tcp::endpoint(*it_endpoint).address(), port);
This is a summarized excerpt of what I did, so it may not compile as-is.
Upvotes: 0