我正在尝试遵循此页面中提供的步骤
http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/tutorial/tutdaytime1.html
但是,在某些时候该代码无法编译,并给了我指定的错误。我不明白这是什么冲突。对象tcpsock似乎没有正确创建。请检查我的代码:
io_service io_tcp;
tcp::resolver resolverObject(io_tcp);
tcp::resolver::query queryObject(argv[1], "daytime");
tcp::resolver::iterator endpoint_iterator = resolverObject.resolve(queryObject);
tcp::resolver::iterator end; //default constructor is end iterator
tcp::socket tcpsock(io_service);
boost::system::error_code socketError = boost::asio::error::host_not_found;
while (socketError && endpoint_iterator != end)
{
//Apparently, the object isn't created correctly
tcpsock.close(); //error happenes here
tcpsock.connect(*endpoint_iterator++, socketError); //error happenes here too
}
if (socketError)
throw boost::system::system_error(socketError);
感谢您的辛勤工作。
最佳答案
您正在使用tcpsock
类型而不是变量io_service
初始化io_tcp
。