在.net中的IP路由表查找路由表、net、IP

2023-09-03 04:14:39 作者:相关优雅好听的昵称>>

我有一个多宿主机器,需要回答这个问题:

I have a multi-homed machine and need to answer this question:

由于远程机器,它的本地接口适用于用于通信的IP地址。

Given an IP address of the remote machine, which local interface is appropriate to use for communication.

这需要在C#中完成的。我可以使用Win32 Socket和SIO_ROUTING_INTERFACE_QUERY,但环顾四周的.NET Framework文档中我还没有找到一个等效为它做查询。

This needs to be done in C#. I can do this query using Win32 Socket and SIO_ROUTING_INTERFACE_QUERY but looking around in the .net framework documentation I haven't found an equivalent for it.

感谢

推荐答案

我不知道这一点,所以才不得不在Visual Studio对象浏览器一看,它看起来像你可以从的System.Net.Sockets 命名空间。

I didn't know about this, so just had a look in the Visual Studio Object browser, and it looks like you can do this from the System.Net.Sockets namespace.

在该命名空间是插槽类,它包含了一个方法的IOControl 。其中一个重载这种方法使用了一个的IOControl code (枚举在同一个命名空间),其中包含了'RoutingInterfaceQuery的条目。

In that namespace is a Socket class which contains a method IOControl. One of the overloads for this method takes an IOControlCode (enum in the same namespace) which contains an entry for `RoutingInterfaceQuery'.

我会尽量把一些code一起作为一个例子吧。

I'll try and put some code together as an example now.