[asio] new resolver query with numeric ports

I'm converting some code from the previous asio release to the latest 0.37 and I am confused by the new resolver interface. ip::tcp::resolver resolver(io_service); ip::tcp::resolver::query query("foobar.com", "http"); ip::tcp::resolver::iterator iter = resolver.resolve(query); ip::tcp::resolver::iterator end; while (iter != end) { ip::tcp::endpoint ep = *iter; std::cout << iter->endpoint() << std::endl; std::cout << iter->host_name() << std::endl; std::cout << iter->service_name() << std::endl; ++iter; } The resolver::query requires a service name string. I want to specify a port number so that the endpoints returned by the resolver::iterator are configured with that specified port rather than those returned by the service name. It isn't clear to me, but it looks like it should be possible by passing numeric_service in the query flags, but on Windows AI_NUMERICSERV isn't defined and numeric_service == 0. Plus that leaks the rather ugly getaddrinfo() interface of using numeric strings. It would be nice if the interface allowed: ip::tcp::resolver::query query("foobar.com", 80); I feel like I might be missing something here. Thanks, Chris

Hi Chris, christopher baus <christopher@baus.net> wrote:
It isn't clear to me, but it looks like it should be possible by passing numeric_service in the query flags, but on Windows AI_NUMERICSERV isn't defined and numeric_service == 0. Plus that leaks the rather ugly getaddrinfo() interface of using numeric strings.
It would be nice if the interface allowed:
ip::tcp::resolver::query query("foobar.com", 80);
Yeah, i didn't provide such an overload to avoid ambiguity between port numbers and flags arguments. Using the string "80" will work on windows even though AI_NUMERICSERV isn't defined by the implementation. You can also pass the numeric_service flag to optimise the call on platforms where it is supported, but it isn't absolutely required to have the numeric string converted. Another approach (although not strictly meeting your requirements) is to use the endpoint::port() setter function after resolution. Cheers, Chris

Another approach (although not strictly meeting your requirements) is to use the endpoint::port() setter function after resolution.
This is the approach I intended to use, but I couldn't figure out how to structure the query. First I tried: ip::tcp::resolver::query query("foobar.com"); But that sets the service to foobar.com, not the host. Then I tried: ip::tcp::resolver::query query("foobar.com", ""); But that failed. The getaddrinfo() function allows for the service string to be null, but there doesn't seem to be a way to achieve that with the asio interface (again unless I'm missing something). Thanks, Chris
participants (2)
-
christopher baus
-
Christopher Kohlhoff