在机器上获取所有IP地址

[英]Get All IP Addresses on Machine


How can I get all of the IP addresses attached to the machine that my application (C# NET Console app) is running on? I need to bind a WCF service to the primary IP address, and return a list of the full IP address list.

如何获取运行我的应用程序(C#NET控制台应用程序)的计算机上连接的所有IP地址?我需要将WCF服务绑定到主IP地址,并返回完整IP地址列表的列表。

using System.Net;

string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();

This is what I am using right now to get the Primary IP address, but I can't figure out how to get the rest to return them.

这就是我现在使用的获取主IP地址,但我无法弄清楚如何让其余的返回它们。

If I bind a WCF service to localhost:8000, will that expose it on the primary?

如果我将WCF服务绑定到localhost:8000,是否会在主服务器上公开它?

6 个解决方案

#1


7  

I think this example should help you.

我认为这个例子可以帮到你。

// Get host name
String strHostName = Dns.GetHostName();

// Find host by name
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// Enumerate IP addresses
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
    ....
}

Edit:

编辑:

"There's no such thing as a "primary" IP address.

“没有”主要“IP地址这样的东西。

The routing table determines which outward-facing IP address is used depending on the destination IP address (and by extension, the network interface, which itself can be virtual or physical)."

路由表根据目标IP地址(以及扩展名为网络接口,可以是虚拟或物理接口)确定使用哪个外向IP地址。

#2


39  

The DNS variants work across the network, but one DNS entry can have many IP addresses and one IP address can have many DNS entries. More importantly, an address needn't be bound to a DNS entry at all.

DNS变体可在网络上运行,但一个DNS条目可以有多个IP地址,一个IP地址可以有多个DNS条目。更重要的是,地址根本不需要绑定到DNS条目。

For the local machine try this:-

对于本地机器试试这个: -

  foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  {
    Console.WriteLine("Name: " + netInterface.Name);
    Console.WriteLine("Description: " + netInterface.Description);
    Console.WriteLine("Addresses: ");
    IPInterfaceProperties ipProps = netInterface.GetIPProperties();
    foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
    {
      Console.WriteLine(" " + addr.Address.ToString());
    }
    Console.WriteLine("");
  }

#3


6  

Why not just bind to 0.0.0.0 ?
That way you listen on all ips

为什么不绑定到0.0.0.0?那样你就听所有的ips

#4


1  

You should probably bind to 0.0.0.0:8000, that will expose it on all available IP addresses and only bind to a particular IP address if the user/administrator demands so.

您应该绑定到0.0.0.0:8000,它将在所有可用的IP地址上公开它,并且只有在用户/管理员要求时才绑定到特定的IP地址。

#5


0  

IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

#6


-2  

I think the OP is asking about how to get all addresses on a local NIC, not just those addresses known to DNS. By primary he probably means the main address under "use the following IP address" in the adapter properties, and by "the rest" he probably means those listed in Advanced > (Additional) IP Addesses.

我认为OP正在询问如何获取本地NIC上的所有地址,而不仅仅是DNS已知的那些地址。通过主要,他可能意味着适配器属性中“使用以下IP地址”下的主要地址,而“其余”可能意味着高级>(附加)IP Addesses中列出的那些。

DNS will not necessarily know those.

DNS不一定知道那些。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/03/11/bb38ab568fe6e723c89f3fdf0c65e2aa.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号