Getting the IP of the interface that received a recvfrom() UDP packet (Microsoft)

偶尔善良 提交于 2020-01-17 01:19:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!