4

使用自制的发现工具,我发现了我感兴趣的服务列表。

我有他们的 IP、服务名称、端口、主机……但要使用它们,我必须向客户端库指定我们将使用的 IP。

由于我有几个网卡,我需要检测哪个接口用于与我知道的目标 IP 进行通信,然后将其提供IPAddress给我的库。

但是我怎么能检测到我应该使用哪个接口 IP 呢?

我试图在互联网上进行一些搜索,但我认为我没有正确的关键字,因为我没有找到任何相关的东西。

4

3 回答 3

10

我已经从我开发的网络库networkComms.net中为您提取了相关方法:

/// <summary>
/// Determines the most appropriate local end point to contact the provided remote end point. 
/// Testing shows this method takes on average 1.6ms to return.
/// </summary>
/// <param name="remoteIPEndPoint">The remote end point</param>
/// <returns>The selected local end point</returns>
public static IPEndPoint BestLocalEndPoint(IPEndPoint remoteIPEndPoint)
{    
    Socket testSocket = new Socket(remoteIPEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
    testSocket.Connect(remoteIPEndPoint);
    return (IPEndPoint)testSocket.LocalEndPoint;
}

一旦您拥有正确的 IP,您就可以遍历NetworkInterface.GetAllNetworkInterfaces()以找到匹配的适配器。

于 2013-02-26T10:50:29.777 回答
1

以下通过查询路由表来工作。这与Socket.Connect确定要使用的本地端点的方式相同。差异:

  • 由于防火墙而不能失败
  • 由于不存在远程端点而不能失败
  • 不保留本地端口
  • 快点

.

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.

于 2014-10-28T15:00:42.397 回答
0

我花了几个小时来找到这个问题的简短答案,并且感谢 BatteryBackupUnit,我已经制作了这个解决方案,并希望它可以帮助其他程序员:

    [DllImport("ws2_32.dll", SetLastError = true)]
    private static extern SocketError WSAIoctl([In] IntPtr socketHandle, uint ioControlCode, [In] byte[] inBuffer, int inBufferSize, [Out] byte[] outBuffer, int outBufferSize, out int bytesTransferred, IntPtr overlapped, IntPtr completionRoutine);

    /// <summary>Get local IP-address for remote address</summary>
    /// <param name="remoteAddress">Remote Address</param>
    /// <returns></returns>
    public static IPAddress GetLocalAddressForRemote(IPAddress remoteAddress)
    {
        if (remoteAddress == null) return null;

        long rm = remoteAddress.Address;
        //Temporary socket only for handle of it
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        byte[] src = new byte[16];

        src[0] = 2;
        src[4] = (byte)rm;
        src[5] = (byte)(rm >> 8);
        src[6] = (byte)(rm >> 16);
        src[7] = (byte)(rm >> 24);

        int transeferred = 0;
        if (WSAIoctl(s.Handle, 3355443220, src, 16, src, 16, out transeferred, IntPtr.Zero, IntPtr.Zero) == SocketError.Success)
        {
            s.Dispose();
            rm = src[4];
            rm |= ((long)src[5]) << 8;
            rm |= ((long)src[6]) << 16;
            rm |= ((long)src[7]) << 24;
            return new IPAddress(rm);
        }
        s.Dispose();
        return null;
    }
于 2018-02-06T08:16:16.660 回答