C++ WinSock 选择本地接口进行连接

C++ WinSock choose local interface for connection

我的系统有多个 NIC,因此有多个 IP 地址,我必须使用一个 SDK,其初始化需要我的本地地址和远程地址。

我想自动选择本地端点。目前我正在枚举所有本地地址(通过 GetAdaptersAddresses)检查最佳匹配(为了正确我应该使用子网掩码)。

但是考虑到这项工作是由路由完成的 table 有没有 Windows API 给定远程地址给我返回正确的本地端点?

is there any Windows API that given a remote address gives me back the right local endpoint ?

看看GetBestInterface() and GetBestInterfaceEx()

两个函数都return接口的索引。然后,您可以通过使用 GetAdaptersInfo()/GetAdaptersAddresses() 枚举网络适配器来检索接口的 IP,直到找到具有匹配接口索引的适配器。