如何判断套接字从哪个接口接收消息? [英] How to tell which interface the socket received the message from?

查看:100
本文介绍了如何判断套接字从哪个接口接收消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果套接字绑定到IN6ADDR_ANYINADDR_ANY,并且您使用诸如recvfrom()之类的调用在套接字上接收消息.有没有办法找出消息来自哪个接口?

If a socket is bound to IN6ADDR_ANY or INADDR_ANY and you use a call such as recvfrom() to receive messages on the socket. Is there a way to find out which interface the message came from?

对于IPv6链接范围消息,我希望recvfrom()的from参数会将scope_id字段初始化为接口ID.不幸的是,在我的测试程序中将其设置为0.

In the case of IPv6 link-scope messages, I was hoping that the from argument of recvfrom() would have the scope_id field initialized to the interface Id. Unfortunately it is set to 0 in my test program.

有人知道找出这种信息的方法吗?

Anybody know of a way to find out this information?

推荐答案

除了绑定到每个接口之外,我还不知道使用IPv4的方式.

Apart from binding to each interface, I'm not aware of a way with IPv4, per se.

IPv6已添加IPV6_PKTINFO套接字选项来解决此缺点.启用该选项后,struct in6_pktinfo将作为辅助数据返回.

IPv6 has added the IPV6_PKTINFO socket option to address this shortcoming. With that option in effect, a struct in6_pktinfo will be returned as ancillary data.

这篇关于如何判断套接字从哪个接口接收消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆