Windows 使用resolver::async\u resolve时出现系统错误955
在使用Windows 使用resolver::async\u resolve时出现系统错误955,windows,boost,boost-asio,system-error,Windows,Boost,Boost Asio,System Error,在使用tcp::resolver中的async_resolve方法时,有时会出现系统错误995。 下面的代码显示了相关的代码行 #include <boost/bind.hpp> #include <boost/asio.hpp> #include <iostream> class connection { public: connection(boost::asio::io_service& io_service) : m
tcp::resolver
中的async_resolve
方法时,有时会出现系统错误995
。
下面的代码显示了相关的代码行
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <iostream>
class connection
{
public:
connection(boost::asio::io_service& io_service)
: m_resolver(io_service), m_socket(io_service)
{
}
void async_connect(
std::string const& host,
std::string const& service,
boost::asio::ip::tcp::socket::protocol_type tcp = boost::asio::ip::tcp::v4()
)
{
m_resolver.async_resolve(
boost::asio::ip::tcp::resolver::query(tcp, host, service),
boost::bind(
&connection::resolve_handler,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator
)
);
}
protected:
virtual void on_connected(boost::system::error_code const& connect_error)
{
if (!connect_error)
{
std::cout << "Connection established. Endpoint: " << m_socket.remote_endpoint()
<< std::endl;
}
else
std::cout << "error: " << connect_error << std::endl;
}
private:
void connect_handler(boost::system::error_code const& connect_error)
{
on_connected(connect_error);
}
void resolve_handler(
boost::system::error_code const& resolve_error,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator
)
{
if (!resolve_error)
{
m_socket.async_connect(
*endpoint_iterator,
boost::bind(
&connection::connect_handler,
this,
boost::asio::placeholders::error
)
);
}
}
private:
boost::asio::ip::tcp::socket m_socket;
boost::asio::ip::tcp::resolver m_resolver;
};
int main()
{
boost::asio::io_service io_service;
connection foo(io_service);
foo.async_connect("www.google.de", "http");
io_service.run();
return 0;
}
#包括
#包括
#包括
类连接
{
公众:
连接(boost::asio::io_服务和io_服务)
:m_解析器(io_服务)、m_插槽(io_服务)
{
}
无效异步连接(
std::字符串常量和主机,
标准::字符串常量和服务,
boost::asio::ip::tcp::socket::protocol_type tcp=boost::asio::ip::tcp::v4()
)
{
m_resolver.async_resolve(
boost::asio::ip::tcp::resolver::query(tcp、主机、服务),
boost::bind(
&连接::解析\u处理程序,
这
boost::asio::占位符::错误,
boost::asio::占位符::迭代器
)
);
}
受保护的:
连接上的虚拟无效(boost::system::error\u code const&connect\u error)
{
如果(!连接错误)
{
std::cout有时我们对最明显的问题视而不见。这就是main
-函数的外观:
int main()
{
boost::asio::io_service io_service;
connection conn(io_service); // Perhaps we should actually declare the object if
conn.async_connect("www.google.de", "http"); // we want to use it beyond this line ...
io_service.run();
return 0;
}
我怀疑你的猜测是对的。你是否在valgrind这样的内存调试器下运行了你的程序?网络通信是脆弱的和有损的。你可能只需要增加一点健壮性。fwiw你的例子没有为我编译。我用一个这样的例子编辑了你的问题。很好。