I'm attempting to make my networked application work locally (with both the server and client running on the same computer) when there is no network connection. This seems to "just work" occasionally, but most of the time I end up with:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): Host not found (authoritative)
Aborted
The code I'm currently using is:
tcp::resolver::query query(host, PORT);
tcp::resolver::iterator endpointIterator = resolver.resolve(query);
tcp::resolver::iterator end;
boost::system::error_code error = boost::asio::error::host_not_found;
while(error && endpointIterator != end)
{
mySocket.close();
mySocket.connect(*endpointIterator++, error);
}
if(error)
throw boost::system::system_error(error);
I'm pretty sure I've narrow the problem down to ip::basic_resolver::resolve, but I can't figure out how that's implemented on Linux or what else I might want to use. This seems to be the same issue. It seems that just not performing any lookup at all and just using 127.0.0.1 should work, but when I tried replacing the query line with
tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::address_configured | boost::asio::ip::resolver_query_base::numeric_host
it didnt' work.
As I was writing this I found my mistake, the address_configured flag (which is set by default) prevents resolve from returning if the loopback device is the only one with an address. I'm still posting this question in the hopes that it helps someone else, but I've solved my issue.
Now I use
tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::numeric_service);
Though other people might not want the flag I'm using if they do want to lookup service names (I'm just using port numbers).