获取机器的所有IP地址 [英] Get All IP Addresses on Machine
问题描述
我怎样才能得到所有我的应用程序(C#NET控制台应用程序)运行在连接到本机的IP地址?我需要WCF服务绑定到主IP地址,并返回完整的IP地址列表中的一个列表。
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.
using System.Net;
string myHostName = Dns.GetHostName().ToString();
string ipAddress = Dns.Resolve(HostName).AddressList[0].ToString();
这是我使用的是什么,现在得到了主IP地址,但我想不出如何让剩下来回报他们。
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.
如果我WCF服务绑定到本地主机:8000,将是暴露在主
If I bind a WCF service to localhost:8000, will that expose it on the primary?
推荐答案
我觉得这个例子可以帮助你。
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)
{
....
}
编辑:
没有这样的东西作为一个主IP地址。
"There's no such thing as a "primary" IP address.
路由表确定哪个朝外取决于目的地IP地址的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地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!