How to allow host (only) using Boost.Asio?

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

What should I do if I want to allow a host without relation to a port? How am I supposed to do this? Should I indicate a dummy port?

+6
c ++ boost boost-asio
source share
1 answer

In one message on the boost mailing list, someone else seemed to do it like this (copied, reformatted, service number changed, nothing more):

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

As you rightly said, the service is still being transmitted here, but the step through the Boost.Asio code showed this (in resolver_service.hpp , I use the rather old version 1.36):

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

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

+7
source share

All Articles