我正在構建一個使用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