本地主机上的哪个网络接口连接到主机 B
假设我的计算机 A 连接到多个网络。其中一个网络上有另一台主机 B。
是否可以通过主机 A 的 C# 来确定主机 B 如何到达主机 A?该解决方案必须同时处理 IPv4 和 IPv6。它不需要处理 NAT。
有人可能会说我想找出主机 A 的哪一个网络接口连接到主机 B。
Let's say my computer A is connected to multiple networks. On one of these networks is another host, B.
Is it possible, through C# from host A, to determine how host B would reach host A? The solution must handle both IPv4 and IPv6. It doesn't need to handle NAT.
One could say I want to figure out which one of host A's network interfaces is connected to host B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
永远不可能知道另一台主机将如何到达您,因为这取决于该主机的路由表。
然而,通常路由是对称的,这意味着远程主机将以与您到达该远程主机相同的方式到达您。您可以检查路由表。在 Windows 上,该命令为“route -n”。这也可能是通过一些 API 公开的。
如果主机 A 和 B 已连接,则该连接将显示在 netstat 中。
如果您控制具有已连接套接字的代码,那么您只需要获取对等点即可。抱歉,我没有 C# 代码来执行此操作。
It is never possible to tell how another host will reach you because it is up to that host's route table.
However, usually routes are symmetric which means a remote host will reach you the same way you will reach that remote host. You can examine the route table. On Windows that command would be "route -n". This is probably exposed via some API too.
If host A and B are already connected, that connection will show up in netstat.
If you control the code that has the connected socket then you just need to get the peer. Sorry I don't have the C# code to do that.
您可能会尝试查看 http://msdn.microsoft。 com/en-us/library/system.net.networkinformation.tcpconnectioninformation.aspx 以及。
You might trying looking at http://msdn.microsoft.com/en-us/library/system.net.networkinformation.tcpconnectioninformation.aspx as well.
你的收获是 如何确定将使用哪个网络接口(IP地址)将数据包发送到特定IP地址?
您可以找到pinvoke GetBestAddress 。我无法找到支持 IPv6 的 GetBestAddressEx 的一些 pinvoke 示例
your catch is How to detemine which network interface (ip address) will be used to send a packet to a specific ip address?
you can find pinvoke for GetBestAddress . i am unable to find some pinvoke example for GetBestAddressEx that supports IPv6