Boost Asio的主机名解析如何在Linux上工作?是否可以使用NSS? [英] How does Boost Asio's hostname resolution work on Linux? Is it possible to use NSS?

查看:101
本文介绍了Boost Asio的主机名解析如何在Linux上工作?是否可以使用NSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在没有网络连接时使我的联网应用程序在本地工作(服务器和客户端都在同一台计算机上运行).这似乎偶尔会起作用",但是大多数时候我都会遇到以下问题:

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

我当前使用的代码是:

  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);

我敢肯定,我已将问题缩小到似乎是同一问题.似乎根本不执行任何查找而仅使用127.0.0.1应该可以,但是当我尝试将查询行替换为

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

没有用. 在撰写本文时,我发现了自己的错误,如果环回设备是唯一一个具有地址的设备,则address_configured标志(默认设置)会阻止解析返回.我仍在发布此问题,希望对其他人有所帮助,但是我已经解决了我的问题.

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.

现在我使用

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).

推荐答案

问题是查询的构造函数默认设置了address_configured标志,如果回送设备是唯一具有地址的设备,则不会返回地址.通过仅将标志设置为0或address_configured以外的任何其他参数,即可解决此问题. 这是我现在成功使用的东西:

The problem was that the constructor for query has the address_configured flag set by default which won't return an address if the loopback device is the only device with an address. By just settings flags to 0 or anything other than address_configured the problem is fixed. Here is what I'm successfully using now:

tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::numeric_service);

希望这对以后解决此问题的人有帮助.

Hope this helps anyone with this problem in the future.

这篇关于Boost Asio的主机名解析如何在Linux上工作?是否可以使用NSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆