我尝试使用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()调用的顺序是否正确?
答案 0 :(得分:0)
代码看起来很好。尝试使用error_code来获取set_option()调用的结果。
boost::system::error_code ec;
_socket.set_option(boost::asio::socket_base::reuse_address(true), ec);