开发者

Boost Asio tcp::resolver: service not found

开发者 https://www.devze.com 2023-03-19 03:01 出处:网络
I\'m trying to create an HTTP client using Boost Asio. I copied the sync client example from asio, compile, then run. Unfortunately, on my logs, it shows Service not found. When I traced the code, I f

I'm trying to create an HTTP client using Boost Asio. I copied the sync client example from asio, compile, then run. Unfortunately, on my logs, it shows Service not found. When I traced the code, I found it that it is thrown from the following code:

boost::asio::io_service io_service;
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
//->if i removed the http, it has no error
tcp::resolver::query query开发者_如何学Go("host.com", "http");
//->This part throws the service not found
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

Can anybody explain why it is throwing service not found or on how can I solve this?


This means the OS does not know which port number corresponds to the TCP service with the name "http".

On a unix-like OS, this would mean the line http 80/tcp is missing from /etc/services, I am able to reproduce the error on Linux by commenting that line out.

If the OS cannot be configured to use services, you may use any service "" in the resolver, and specify the port number explicitly when creating the endpoint object for the connect call:

tcp::endpoint connectionEndpoint(endpoint_iterator->address(), 80);
boost::system::error_code ec;
socket.connect(connectionEndpoint, ec);
0

精彩评论

暂无评论...
验证码 换一张
取 消