The debug loop is exhausting the iterator, causing boost::asio::connect()
to be invoked with an end iterator.
boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, ec);
while(iter != iterEnd) // iterate to exhaustion
{
ep = *iter++;
// ...
}
// ...
boost::asio::connect(sock, iter, ec); // use exhausted iterator
To resolve this, consider iterating over a copy of iter
when building up the debug information by changing:
while(iter != iterEnd)
{
ep = *iter++;
// ...
}
to:
for(tcp::resolver::iterator i = iter, end; i != end; ++i)
{
ep = *i;
// ...
}
Here is a complete example based roughly on the original code which demonstrates the differences:
#include <iostream>
#include <boost/asio.hpp>
void connect(const char* address, const char* port, bool exhaust)
{
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::iterator iterator = resolver.resolve(
tcp::resolver::query(address, port));
// If flag is set, exhaust the iterator.
if (exhaust)
{
std::cout << "Exhausting iterator" << std::endl;
for (tcp::resolver::iterator end; iterator != end; ++iterator) {}
}
// Otherwise, exhaust a copy of the iterator.
else
{
std::cout << "Exhausting copy of iterator" << std::endl;
for (tcp::resolver::iterator i = iterator, end; i != end; ++i) {}
}
tcp::socket socket(io_service);
boost::system::error_code error;
connect(socket, iterator, error);
std::cout << error.category().name() << " -- "
<< error.message() << std::endl;
}
int main()
{
connect("google.com", "80", true); // exhaust
connect("google.com", "80", false); // do not exhaust
}
Which produces the following output:
Exhausting iterator
asio.misc -- Element not found
Exhausting copy of iterator
system -- Success