获取机器上的所有 IP 地址 [英] Get All IP Addresses on Machine

查看:32
本文介绍了获取机器上的所有 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取连接到运行我的应用程序(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 服务绑定到 localhost: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆