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("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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With