获取接收recvfrom() UDP数据包的接口的IP (Microsoft)
在 Microsoft 多宿主 PC 上绑定到 INADDR_ANY 的套接字上使用 recvfrom()。 当recvfrom()获取UDP数据包时:如何找到接收数据包的接口(IP)?
Using recvfrom() on a socket bound to INADDR_ANY on a Microsoft multihomed PC.
when recvfrom() gets an UDP packet: how can I find the Interface (IP) that received the packet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当单个侦听套接字绑定到多个 IP 时,无法知道接收 IP。您可以使用
GetAdaptersInfo()
和/或GetAdapterAddresses()
查询计算机的本地 IP 列表,而不是将单个套接字绑定到INADDR_ANY
,然后为每个IP创建一个单独的监听套接字。您可以使用 getsockname() 来了解给定套接字绑定到哪个 IP,但前提是该套接字绑定到特定 IP,而不是多个 IP。There is no way to know the receiving IP when a single listening socket is bound to multiple IPs. Instead of binding a single socket to
INADDR_ANY
, you can query the machine's list of local IPs usingGetAdaptersInfo()
and/orGetAdapterAddresses()
, then create a separate listening socket for each IP. You can usegetsockname()
to know which IP a given socket is bound to, but only when that socket is bound to a specific IP, not multiple IPs.