gpt4 book ai didi

sockets - 使用 boost asio 重用套接字

转载 作者:行者123 更新时间:2023-12-01 05:36:21 25 4
gpt4 key购买 nike

我尝试使用绑定(bind)到本地地址/端口组合的 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);

关于sockets - 使用 boost asio 重用套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com