Finally, let's take a look at one of the more frequently asked questions about sending UDP/IP messages on machines with multiple network interfaces: What happens when a UDP socket is bound explicitly to a local IP interface and datagrams are sent? With UDP sockets, you don't really bind to the network interface; you create an association wherebythe IP interface that is bound becomes the source IP address of UDP datagrams sent. The routing table actually determines which physical interface the datagram is transmitted on. If you do not call bind but instead use either sendto/WSASendTo or perform a connect first, the network stack automatically picks the best local IP address based on the routing table. So if you explicitly bind first, the source IP address could be incorrect. That is, the source IP might not be the IP address of the interface on which the datagram was actually sent.

解决方案 »

  1.   

    最后,让我们看一下问得最多的关于在一部拥有多个网络接口的主机如何发送UDP/IP信息:当一个UDP套接字绑定到一个本地的IP接口时会发生什么事情,将会发送什么数据报文?对于UDP套接字来说,你不需要真的把它绑定到一个网络接口;你创建的关联(与一个IP绑定)将会成为发送数据报文的源IP地址.路由表将会决定数据报文在那一个物理接口进行传送.如果你没有绑定IP,然后使用sendto / WSASendTo或者先执行一个连接的话,网络栈会自动根据路由表选择一个最好的本地IP地址.所以,如果你一开始就绑定一个IP的话,那个源IP地址可能是不正确的.意思是说,这个源IP地址可能不是数据报文传送实际用到的那个IP地址.这是以我的英文水平翻译的,希望你能看得明白.