我有一个具有多个 NIC 和多个 IP 地址的系统,我必须使用一个 SDK,其初始化需要我的本地地址和远程地址。
我想自动选择本地端点。目前我正在枚举所有本地地址(通过 GetAdaptersAddresses
)检查最佳匹配(为了正确我应该使用子网掩码)。
但是考虑到这项工作是由路由表完成的,是否有任何提供远程地址的 Windows API 返回正确的本地端点?
最佳答案
is there any Windows API that given a remote address gives me back the right local endpoint ?
看看GetBestInterface()
和 GetBestInterfaceEx()
.
这两个函数都返回一个接口(interface)的索引。然后,您可以通过使用 GetIpAddrTable()
枚举网络接口(interface)来检索接口(interface)的 IP。 ,或使用 GetAdaptersInfo()
枚举网络适配器/GetAdaptersAddresses()
, 直到找到具有匹配接口(interface)索引的条目。
关于C++ WinSock 选择本地接口(interface)进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919742/