gpt4 book ai didi

networking - 将 boost asio 绑定(bind)到本地 tcp 端点

转载 作者:行者123 更新时间:2023-12-04 02:20:15 25 4
gpt4 key购买 nike

我正在尝试将 boost asio tcp 套接字专门绑定(bind)到本地网络接口(interface)。在套接字上调用 bind() 方法的正确时间是什么时候?

_endpoint 指向远程 ip/端口,例如192.168.0.15:8888.

// Invoke async. connect. Immediate return, no throw.
_socket.async_connect(_endpoint,
boost::bind(&MyTransceiver::handleConnect, this,
boost::asio::placeholders::error));

MyTransceiver::handleConenct() 中,我尝试了以下代码:

boost::asio::ip::tcp::endpoint local_end_point(
boost::asio::ip::address::from_string("192.168.0.55"), 6543 );

_socket.bind(local_end_point);

在此处调用它失败,在 async_connect() 调用之前调用它,并且出现“无效句柄”异常。

最佳答案

似乎没有足够的信息。但一般来说,您应该:

_socket->打开()

_socket->set_option()

_socket->绑定(bind)()

_socket->async_connect()

在 handleConnect(): _socket->async_read_some()

关于networking - 将 boost asio 绑定(bind)到本地 tcp 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416917/

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