使用 boost asio udp 套接字时如何设置本地端点

发布于 2024-11-19 14:52:33 字数 477 浏览 4 评论 0原文

我的电脑上有 3 个网络接口,想要确保当我执行 udp 套接字发送时,它通过特定的网络接口发送(我有发送数据时使用的 IP 地址)。

这是代码。

udp::socket send_socket(io_service,boost::asio::ip::udp::v6());
udp::endpoint local_end_point(boost::asio::ip::address::from_string(LocalIpAddress),1111);
//  send_socket.bind(local_end_point);
send_socket.send_to(const_buffer,senderEndpoint,0,error);

上面的代码有效,但我无法控制数据将通过哪个网络接口发送。如果我取消注释 send_socket.bind 行,我将停止在另一端接收任何数据。

还有其他方法可以将套接字绑定到特定的网络接口吗?

I have 3 network interfaces on pc and want to make sure that when I do udp socket send, it sends via a specific network interface ( I have the ip address to use when sending data).

Here is the code.

udp::socket send_socket(io_service,boost::asio::ip::udp::v6());
udp::endpoint local_end_point(boost::asio::ip::address::from_string(LocalIpAddress),1111);
//  send_socket.bind(local_end_point);
send_socket.send_to(const_buffer,senderEndpoint,0,error);

The above code works but I dont have control over what network interface the data would be sent through. If I uncomment the send_socket.bind line, I stop receiving any data on the other end.

Is there any other way to bind a socket to a particular network interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-11-26 14:52:33

bind() 函数将您的套接字绑定到特定的网络接口以进行发送和接收。如果您停止在另一端接收数据,可能是因为另一端无法通过您在 bind 调用中指定的 NIC 进行路由。

The bind() function ties your socket to a specific network interface for sending AND receiving. If you stop receiving data on the other end it's likely because the other end is not routable via the NIC that you've specified in the bind call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文