获取绑定到 INADDR_ANY 的 UDP 套接字接口的 IP 地址
Get IP address of Interface for a UDP socket when it is bound to INADDR_ANY
当我们将 UDP 服务器套接字(Ip/端口)绑定到一个特殊的 IP 地址时 INADDR_ANY - 系统中所有可用的接口都将用于端口 PORT。
现在实际上我需要在客户端连接时找到接口的 MTU - 所以我需要找到客户端连接到它的接口详细信息。
我尝试使用 getsockname
API 但它返回“0.0.0.0”。如何获取客户端已连接到的接口的实际 IP?
UDP 中没有连接,这就是为什么你不能按你想要的方式使用 getsockname()
。
如果您想知道绑定到 INADDR_ANY
时哪个接口接收每个数据报,您可以在套接字上启用 IP_PKTINFO
选项,然后使用 recvmsg()
rather than recvfrom()
to read the datagrams. The msghdr
struct that recvmsg()
populates on output can be used to determine the receiving interface. See the cmsg
macros 获取更多详细信息。
见Get destination address of a received UDP packet
当我们将 UDP 服务器套接字(Ip/端口)绑定到一个特殊的 IP 地址时 INADDR_ANY - 系统中所有可用的接口都将用于端口 PORT。
现在实际上我需要在客户端连接时找到接口的 MTU - 所以我需要找到客户端连接到它的接口详细信息。
我尝试使用 getsockname
API 但它返回“0.0.0.0”。如何获取客户端已连接到的接口的实际 IP?
UDP 中没有连接,这就是为什么你不能按你想要的方式使用 getsockname()
。
如果您想知道绑定到 INADDR_ANY
时哪个接口接收每个数据报,您可以在套接字上启用 IP_PKTINFO
选项,然后使用 recvmsg()
rather than recvfrom()
to read the datagrams. The msghdr
struct that recvmsg()
populates on output can be used to determine the receiving interface. See the cmsg
macros 获取更多详细信息。
见Get destination address of a received UDP packet