boost::asio::ip::tcp::resolver::iterator 检查值是否为空
boost::asio::ip::tcp::resolver::iterator Check if values are null
我有一段代码在执行时抛出访问冲突。它是 boost::asio 的 async_connect 处理程序。
//------------------------------------------------------------------------------
void MyClient::OnConnect(const boost::system::error_code & errorCode, boost::asio::ip::tcp::resolver::iterator endpoint)
{
if (errorCode || endpoint == boost::asio::ip::tcp::resolver::iterator())
{
// Error - An error occured while attempting to connect
// Most likely these error codes correspond to https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
std::ostringstream msg;
msg << "An error occured while attempting to connect to " << endpoint->host_name()
<< ". Error code: " << errorCode
<< ". Error Message: " << ErrorCodeToString(errorCode);
LogError(msg.str().c_str(), __FUNCSIG__, __FILE__, __LINE__);
return;
}
// We connected to an endpoint
m_connectionState |= CONNECTED;
在调试器中,问题似乎出在 endpoint->host_name() 中,因为它试图获取 values_[0] 而 values_ 为空。
这是一种常见的连接被拒绝的情况。我以为处理程序得到了端点,所以它知道它试图连接到谁!在尝试调用迭代器上的方法之前,我可以对迭代器进行某种检查吗?
似乎通过了,但仍然在
上抛出访问冲突
if( endpoint != boost::asio::ip::tcp::resolver::iterator() )
{
std::string blah = endpoint->host_name();
}
可能你现在已经想通了,但我会 post 几行以防其他人碰巧遇到它。
这个问题和我遇到的问题很相似。
这 不会 起作用,并且会产生与您所描述的相似的结果。
void establish_connection() {
tcp::resolver resolver(get_asio_io_service()); // this is a problem
tcp::resolver::query q{server_name, "https"};
resolver.async_resolve(q,
[&](asio::error_code ec,
tcp::resolver::iterator it) {
this->handle_resolve(ec, it);
});
}
其中 establish_connection() 是对象中处理与服务器通信的方法。
解析器对象在 establish_connection() 退出后消失。你必须找到一种方法让它坚持下去。网上各种demo都把它作为client对象的一个属性。
请分享您对此问题的经验。
我有一段代码在执行时抛出访问冲突。它是 boost::asio 的 async_connect 处理程序。
//------------------------------------------------------------------------------
void MyClient::OnConnect(const boost::system::error_code & errorCode, boost::asio::ip::tcp::resolver::iterator endpoint)
{
if (errorCode || endpoint == boost::asio::ip::tcp::resolver::iterator())
{
// Error - An error occured while attempting to connect
// Most likely these error codes correspond to https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
std::ostringstream msg;
msg << "An error occured while attempting to connect to " << endpoint->host_name()
<< ". Error code: " << errorCode
<< ". Error Message: " << ErrorCodeToString(errorCode);
LogError(msg.str().c_str(), __FUNCSIG__, __FILE__, __LINE__);
return;
}
// We connected to an endpoint
m_connectionState |= CONNECTED;
在调试器中,问题似乎出在 endpoint->host_name() 中,因为它试图获取 values_[0] 而 values_ 为空。
这是一种常见的连接被拒绝的情况。我以为处理程序得到了端点,所以它知道它试图连接到谁!在尝试调用迭代器上的方法之前,我可以对迭代器进行某种检查吗?
似乎通过了,但仍然在
上抛出访问冲突if( endpoint != boost::asio::ip::tcp::resolver::iterator() )
{
std::string blah = endpoint->host_name();
}
可能你现在已经想通了,但我会 post 几行以防其他人碰巧遇到它。
这个问题和我遇到的问题很相似。
这 不会 起作用,并且会产生与您所描述的相似的结果。
void establish_connection() {
tcp::resolver resolver(get_asio_io_service()); // this is a problem
tcp::resolver::query q{server_name, "https"};
resolver.async_resolve(q,
[&](asio::error_code ec,
tcp::resolver::iterator it) {
this->handle_resolve(ec, it);
});
}
其中 establish_connection() 是对象中处理与服务器通信的方法。
解析器对象在 establish_connection() 退出后消失。你必须找到一种方法让它坚持下去。网上各种demo都把它作为client对象的一个属性。
请分享您对此问题的经验。