c++ - 如何使用 Boost.Asio 解析主机(仅)?

标签 c++ boost boost-asio

根据documentation boost::asio::ip::tcp::resolver::query 为了解析它应该接收服务的主机

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

最佳答案

one post in the boost mailing list其他人似乎是这样做的(复制、重新格式化、更改服务编号,没有别的):

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 版本) :

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

因此,只需使用 0,它就会按照您的要求进行操作。

关于c++ - 如何使用 Boost.Asio 解析主机(仅)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1516300/

相关文章:

c++ - 为什么 std::auto_ptr operator = 垃圾对象?

c++ - 如何在 C++ 中重载运算符 ->*

c++ - 如何在线程完成时使用 boost::thread::at_thread_exit 或调用函数

c++ - 并行计算大 vector 的总和

c++ - 如何在 Windows 中使用 boost asio 从命令行异步读取输入?

c++ - 将函数指针的 "part"作为参数传递

C++ std::hash_map:键的作用是什么

c++ - boost::transform_iterator 不适用于 std::bind( &Pair::first, _1 )?

c++ - 使用 MPFR 时如何在 Boost Multi precision 中设置舍入模式

c++ - 为什么我的 io_service::run_one() 实现会导致无限期阻塞并触发错误 #125?