获取接收到 recvfrom() UDP 数据包的接口的 IP (Microsoft) [英] Getting the IP of the interface that received a recvfrom() UDP packet (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?
推荐答案
当单个侦听套接字绑定到多个 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 using GetAdaptersInfo()
and/or GetAdapterAddresses()
, then create a separate listening socket for each IP. You can use getsockname()
to know which IP a given socket is bound to, but only when that socket is bound to a specific IP, not multiple IPs.
这篇关于获取接收到 recvfrom() UDP 数据包的接口的 IP (Microsoft)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!