Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does boost::asio::ip::tcp::resolver::iterator do?

I'm starting with boost asio programming in C++ and when looking over the examples I just can't understand what does boost::asio::ip::tcp::resolver::iterator do.

Code:

boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1]);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
  socket.close();
  socket.connect(*endpoint_iterator++, error);
}

Please help me and excuse me if my question doesn't provide enough information.

like image 568
Hami Avatar asked Feb 24 '11 20:02

Hami


1 Answers

boost::asio::ip::tcp::resolver::iterator iterates through the address list of the host that you specified (hosts can have multiple addresses).

Like an std::string::iterator iterates through its characters, boost::asio::ip::tcp::resolver::iterator iterates through its address list.

The following code:

while (error && endpoint_iterator != end)
{
  socket.close();
  socket.connect(*endpoint_iterator++, error);
}

is attempting to establish a connection to each endpoint until it succeeds or runs out of endpoints (thank you for the correction Eugen Constantin Dinca).

like image 113
Marlon Avatar answered Oct 24 '22 04:10

Marlon