获取机器上的所有IP地址

发布于 2024-10-21 20:54:08 字数 357 浏览 1 评论 0原文

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

using System.Net;

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

这就是我现在用来获取主 IP 地址的方法,但我不知道如何让其余的地址返回它们。

如果我将 WCF 服务绑定到 localhost:8000,是否会将其暴露在主服务器上?

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();

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

高跟鞋的旋律 2024-10-28 20:54:08

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

对于本地机器,请尝试以下操作:

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("");
}

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.

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("");
}
遇见了你 2024-10-28 20:54:08

我认为这个例子应该对你有帮助。

// 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 地址之类的东西。

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

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.

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)."

a√萤火虫的光℡ 2024-10-28 20:54:08

为什么不直接绑定到 0.0.0.0 ?
这样你就可以监听所有的ip

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

风尘浪孓 2024-10-28 20:54:08

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

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.

甲如呢乙后呢 2024-10-28 20:54:08
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
似最初 2024-10-28 20:54:08

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

DNS 不一定知道这些。

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.

DNS will not necessarily know those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文