我正在构建一个使用boost::asio
的Socket类的过程。首先,我制作了一个采用主机和端口并将其解析为IP地址的connect
方法。这工作得很好,所以我决定看看async_resolve
。但是,我的回调总是得到一个错误代码995
(使用与同步工作时相同的目标主机/端口)。boost :: asio :: async_resolve问题
代码:
功能启动分辨率:
// 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
代码调用该函数:
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
的error_code
的message()
功能是:
The I/O operation has been aborted because of either a thread exit or an application request
而且我main.cpp
看起来是这样的:
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;
}
提前感谢!
谢谢非常多,应该已经完全明显:) – 2011-01-09 01:13:06