boost::asio异步TCP客户端async_read_some回调函数返回Operation canceled错误
初学boost::asio,服务端是异步的,客户端也是异步的(分别在两台主机),但是async_read_some的回调函数返回Operation canceled错误。把代码改成同步的测试,可以正常read到数据。服务端和客户端在一台主机时,可以read,但是偶现Operation canceled错误。
部分代码:
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;
}
谁知道是什么原因啊