boost::asio::async_resolve Problem
Posted
by
Moo-Juice
on Stack Overflow
See other posts from Stack Overflow
or by Moo-Juice
Published on 2011-01-08T22:49:53Z
Indexed on
2011/01/08
22:53 UTC
Read the original article
Hit count: 289
Hi All,
I'm in the process of constructing a Socket class that uses boost::asio
. To start with, I made a connect
method that took a host and a port and resolved it to an IP address. This worked well, so I decided to look in to async_resolve
. However, my callback always gets an error code of 995
(using the same destination host/port as when it worked synchronously).
code:
Function that starts the resolution:
// resolve a host asynchronously
template<typename ResolveHandler>
void resolveHost(const String& _host, Port _port, ResolveHandler _handler) const
{
boost::asio::ip::tcp::endpoint ret;
boost::asio::ip::tcp::resolver::query query(_host, boost::lexical_cast<std::string>(_port));
boost::asio::ip::tcp::resolver r(m_IOService);
r.async_resolve(query, _handler);
}; // eo resolveHost
Code that calls this function:
void Socket::connect(const String& _host, Port _port)
{
// Anon function for resolution of the host-name and asynchronous calling of the above
auto anonResolve = [this](const boost::system::error_code& _errorCode,
boost::asio::ip::tcp::resolver_iterator _epIt)
{
// raise event
onResolve.raise(SocketResolveEventArgs(*this, !_errorCode ? (*_epIt).host_name() : String(""), _errorCode));
// perform connect, calling back to anonymous function
if(!_errorCode)
connect(*_epIt);
};
// Resolve the host calling back to anonymous function
Root::instance().resolveHost(_host, _port, anonResolve);
}; // eo connect
The message()
function of the error_code
is:
The I/O operation has been aborted because of either a thread exit or an application request
And my main.cpp
looks like this:
int _tmain(int argc, _TCHAR* argv[])
{
morse::Root root;
TextSocket s;
s.connect("somehost.com", 1234);
while(true)
{
root.performIO(); // calls io_service::run_one()
}
return 0;
}
Thanks in advance!
© Stack Overflow or respective owner