C# 识别活动网络接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:28:29  来源:igfitidea点击:

Identifying active network interface

c#.netnetworking

提问by Tiberiu Ana

In a .NET application, how can I identify which network interface is used to communicate to a given IP address?

在 .NET 应用程序中,如何确定使用哪个网络接口与给定的 IP 地址进行通信?

I am running on workstations with multiple network interfaces, IPv4 and v6, and I need to get the address of the "correct" interface used for traffic to my given database server.

我在具有多个网络接口、IPv4 和 v6 的工作站上运行,我需要获取用于到我给定数据库服务器的流量的“正确”接口的地址。

采纳答案by Yariv

The simplest way would be:

最简单的方法是:

UdpClient u = new UdpClient(remoteAddress, 1);
IPAddress localAddr = ((IPEndPoint)u.Client.LocalEndPoint).Address;

Now, if you want the NetworkInterface object you do something like:

现在,如果您想要 NetworkInterface 对象,您可以执行以下操作:


foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
   IPInterfaceProperties ipProps = nic.GetIPProperties();
   // check if localAddr is in ipProps.UnicastAddresses
}


Another option is to use P/Invoke and call GetBestInterface()to get the interface index, then again loop over all the network interfaces. As before, you'll have to dig through GetIPProperties()to get to the IPv4InterfaceProperties.Indexproperty).


另一种选择是使用 P/Invoke 并调用GetBestInterface()来获取接口索引,然后再次循环遍历所有网络接口。和以前一样,您必须深入挖掘GetIPProperties()才能到达该IPv4InterfaceProperties.Index物业)。

回答by Paul Nearney

The info you are after will be in WMI.

您所追求的信息将在 WMI 中。

This example using WMI may get you most of the way:

这个使用 WMI 的示例可能会让您了解大部分情况:

using System.Management;
string query = "SELECT * FROM Win32_NetworkAdapterConfiguration";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();// Every record in this collection is a network interface
foreach (ManagementObject mo in moCollection)
{    
    // Do what you need to here....
}

The Win32_NetworkAdapterConfigurationclass will give you info about the configuration of your adapters e.g. ip addresses etc.

Win32_NetworkAdapterConfiguration的教室将给你提供有关您的适配器,如IP地址等的配置

You can also query the Win32_NetworkAdapterclass to find out 'static'about each adapter (max speed, manufacturer etc)

您还可以查询Win32_NetworkAdapter类以找出每个适配器的“静态”(最大速度、制造商等)

回答by Oliver Friedrich

At least you can start with that, giving you all addresses from dns for the local machine.

至少你可以从这个开始,为你提供来自本地机器的 dns 的所有地址。

IPHostEntry hostEntry = Dns.GetHostEntry(Environment.MachineName);

foreach (System.Net.IPAddress address in hostEntry.AddressList)
{
    Console.WriteLine(address);
}

回答by Coderer

Neither of these will actually give the OP the info he's looking for -- he wants to know which interface will be used to reach a given destination. One way of doing what you want would be to shell out to the routecommand using System.Diagnostics.Process class, then screen-scrape the output. route PRINT (destination IP)will get you something useable. That's probably not the bestsolution, but it's the only one I can give you right now.

这些实际上都不会向 OP 提供他正在寻找的信息——他想知道将使用哪个接口来到达给定的目的地。做你想做的一种方法是使用 System.Diagnostics.Process 类对route命令进行shell ,然后屏幕抓取输出。 route PRINT (destination IP)会给你一些有用的东西。这可能不是最好的解决方案,但这是我现在可以给你的唯一解决方案。

回答by lxa

Just to give a complete picture: another approach would be to use Socket.IOControl( SIO_ROUTING_INTERFACE_QUERY, ... )

只是为了提供一个完整的图片:另一种方法是使用 Socket.IOControl( SIO_ROUTING_INTERFACE_QUERY, ... )

ConferenceXP includes rather comprehensive function wrapping this, works with IPv4/6 and multicast addresses: https://github.com/conferencexp/conferencexp/blob/master/MSR.LST.Net.Rtp/NetworkingBasics/utility.cs#L84

ConferenceXP 包括相当全面的功能包装,适用于 IPv4/6 和多播地址:https: //github.com/conferencexp/conferencexp/blob/master/MSR.LST.Net.Rtp/NetworkingBasics/utility.cs#L84