我有一个多宿主机器,需要回答这个问题:IP路由表查找.net
给定远程机器的IP地址,哪个本地接口适合用于通信。
这需要在C#中完成。我可以使用Win32套接字和SIO_ROUTING_INTERFACE_QUERY来做这个查询,但是在.net框架文档中查找我还没有找到它的等价物。
我有一个多宿主机器,需要回答这个问题:IP路由表查找.net
给定远程机器的IP地址,哪个本地接口适合用于通信。
这需要在C#中完成。我可以使用Win32套接字和SIO_ROUTING_INTERFACE_QUERY来做这个查询,但是在.net框架文档中查找我还没有找到它的等价物。
我不知道这个,所以只需看一下Visual Studio Object浏览器,它看起来像你可以从System.Net.Sockets
命名空间这样做。
在该名称空间中是一个Socket
类,其中包含方法IOControl
。此方法的重载之一需要IOControlCode
(枚举在同一个命名空间中),其中包含“RoutingInterfaceQuery”的条目。
我现在试着将一些代码作为一个例子。
谢谢 - 应该这样做。 – 2013-03-25 21:43:32
这个例子会有很大的帮助;-) – BatteryBackupUnit 2014-08-27 09:33:07
有人不够好,writez代码,看看https://searchcode.com/codesearch/view/7464800/
private static IPEndPoint QueryRoutingInterface(
Socket socket,
IPEndPoint remoteEndPoint)
{
SocketAddress address = remoteEndPoint.Serialize();
byte[] remoteAddrBytes = new byte[address.Size];
for (int i = 0; i < address.Size; i++) {
remoteAddrBytes[i] = address[i];
}
byte[] outBytes = new byte[remoteAddrBytes.Length];
socket.IOControl(
IOControlCode.RoutingInterfaceQuery,
remoteAddrBytes,
outBytes);
for (int i = 0; i < address.Size; i++) {
address[i] = outBytes[i];
}
EndPoint ep = remoteEndPoint.Create(address);
return (IPEndPoint)ep;
}
这是用来像
(例子):
IPAddress remoteIp = IPAddress.Parse("192.168.1.55");
IpEndPoint remoteEndPoint = new IPEndPoint(remoteIp, 0);
Socket socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPEndPoint localEndPoint = QueryRoutingInterface(socket, remoteEndPoint);
Console.WriteLine("Local EndPoint is: {0}", localEndPoint);
请注意,虽然一个与端口指定IpEndPoint
,港口是无关紧要的。此外,返回的IpEndPoint.Port
始终为0
。
我还没有设法得到一个套接字工作提供路由表,但这可能让你开始(如果你需要在正确的方向点) - http://www.winsocketdotnetworkprogramming.com/ – 2013-03-26 22:35:35