问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/7916690/getting-the-ip-of-the-interface-that-received-a-recvfrom-udp-packet-microsoft