部分代码:
void start(){
sock_ptr sock(new ip::tcp::socket(ios));
sock->async_connect(ep, bind(&client::conn_handler, this, placeholders::error, sock));
}
void conn_handler(const boost::system::error_code& ec, sock_ptr sock){
if(ec) return;
cout << "receive from " << sock->remote_endpoint().address() << endl;
boost::shared_ptr<vector<char> > str(new vector<char>(100, 0));
sock->async_read_some(buffer(*str), bind(&client::read_handler, this, placeholders::error, str));
//sync timer block to control the client connection frequency
deadline_timer t(ios, boost::posix_time::seconds(5));
t.wait();
start(); //retry to connect in the next time
}
void read_handler(const boost::system::error_code& ec, boost::shared_ptr<vector<char> > str){
if(ec)
{
cout << "error:";
throw boost::system::system_error(ec);
return;
}
cout << &(*str)[0] << endl;
}
谁知道是什么原因啊
1 个解决方案
#1
[async_connect传递给回调函数conn_handler的参数sock在async_read_some注册了回调函数之后,就过期了,连接被关闭,之后触发了该连接的所有异步回调函数,得到错误码 125,对应的描述是Operation canceled]
#1
[async_connect传递给回调函数conn_handler的参数sock在async_read_some注册了回调函数之后,就过期了,连接被关闭,之后触发了该连接的所有异步回调函数,得到错误码 125,对应的描述是Operation canceled]