如何使用Boost.Asio解析主机(仅)? [英] How to resolve host (only) using Boost.Asio?

查看:35
本文介绍了如何使用Boost.Asio解析主机(仅)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档 boost :: asio :: ip :: ip :: tcp :: resolver :: query 为了解析主机,它应该接收服务也是

According to the documentation of boost::asio::ip::tcp::resolver::query in order to resolve host it should receive service as well.

如果我想解析与端口无关的主机怎么办?我该怎么办呢?应该我指定虚拟端口吗?

What if I want to resolve host without relation to port? How should I do it at all? Should I specify dummy port?

推荐答案

boost邮件列表中的一个帖子似乎有人这样做(复制,重新格式化,服务编号已更改,没有其他内容):

In one post in the boost mailing list somebody else seemed to do it like this (copied, reformatted, service-number changed, nothing else):

namespace bai = boost::asio::ip;
bai::tcp::endpoint ep(bai::address_v4(0xD155AB64), 0); // 209.85.171.100:0
boost::asio::io_service ios;
bai::tcp::resolver resolver(ios);
bai::tcp::resolver::iterator iter = resolver.resolve(ep);
bai::tcp::resolver::iterator end;
while (iter != end)
{
  std::cerr << (*iter).host_name() << std::endl; // cg-in-f100.google.com
  ++iter;
} 

正如您正确地说的那样,这里仍然传递了一项服务,但是通过Boost.Asio代码的一步揭示了这一点(在 resolver_service.hpp 中,我使用的是相当老的1.36版本):

As you correctly said, here a service is still passed in, but a step through the Boost.Asio code revealed this (in resolver_service.hpp, I'm using the rather old 1.36 release):

// First try resolving with the service name. If that fails try resolving
// but allow the service to be returned as a number.

所以,只要加上0,它就可以执行您想要的操作.

So, just go with 0, and it should do what you want.

这篇关于如何使用Boost.Asio解析主机(仅)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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