我尝试使用 boost asio 套接字,绑定到本地地址/端口组合。效果很好。不起作用的是,一旦套接字和应用程序停止并重新启动,就重新使用套接字。
//
// open the socket - it would also be opened by the async_connect()
// method but we might need an open socket to bind it
_socket.open(boost::asio::ip::tcp::v4());
if ( _bindLocal ) {
boost::asio::socket_base::reuse_address option(true);
_socket.set_option(option);
_socket.bind( _localEndpoint );
}
// Invoke async. connect. Immediate return, no throw.
_socket.async_connect(_remoteEndpoint,
boost::bind(&MyTransceiver::handleConnect, this,
boost::asio::placeholders::error));
我缺少什么? open()、set_option() 和bind() 调用的顺序是否正确?
代码看起来不错。尝试使用 error_code 来获取 set_option() 调用的结果。
boost::system::error_code ec;
_socket.set_option(boost::asio::socket_base::reuse_address(true), ec);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)