C# 从 Dns.GetHostEntry() 获取 IPv4 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1059526/
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
Get IPv4 addresses from Dns.GetHostEntry()
提问by zombat
I've got some code here that works great on IPv4 machines, but on our build server (an IPv6) it fails. In a nutshell:
我这里有一些在 IPv4 机器上运行良好的代码,但在我们的构建服务器(IPv6)上它失败了。简而言之:
IPHostEntry ipHostEntry = Dns.GetHostEntry(string.Empty);
The documentation for GetHostEntry says that passing in string.Empty will get you the IPv4 address of the localhost. This is what I want. The problem is that it's returning the string "::1:" on our IPv6 machine, which I believe is the IPv6 address.
GetHostEntry 的文档说,传入 string.Empty 将获得本地主机的 IPv4 地址。这就是我要的。问题是它在我们的 IPv6 机器上返回字符串“::1:”,我认为这是 IPv6 地址。
Pinging the machine from any other IPv4 machine gives a good IPv4 address... and doing a "ping -4 machinename"from itself gives the correct IPv4 address.... but pinging it regularly from itself gives "::1:".
从任何其他 IPv4 机器 ping 机器会给出一个好的 IPv4 地址......并且从它自己做一个“ping -4 machinename”会给出正确的 IPv4 地址......但是定期从它自身 ping 它会给出“::1:”。
How can I get the IPv4 for this machine, from itself?
我怎样才能从它本身获取这台机器的 IPv4?
采纳答案by Remus Rusanu
Have you looked at all the addressesin the return, discard the ones of family InterNetworkV6and retain only the IPv4 ones?
您是否查看了返回中的所有地址,丢弃了家庭 InterNetworkV6 的地址并仅保留了 IPv4 的地址?
回答by Naveen Desosha
public Form1()
{
InitializeComponent();
string myHost = System.Net.Dns.GetHostName();
string myIP = null;
for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
{
if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
{
myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
}
}
}
Declare myIP and myHost in public Variable and use in any function of the form.
在 public 变量中声明 myIP 和 myHost 并在任何形式的函数中使用。
回答by Gary
To find all local IPv4 addresses:
要查找所有本地 IPv4 地址:
IPAddress[] ipv4Addresses = Array.FindAll(
Dns.GetHostEntry(string.Empty).AddressList,
a => a.AddressFamily == AddressFamily.InterNetwork);
or use Array.Find
or Array.FindLast
if you just want one.
或者使用Array.Find
或者Array.FindLast
如果你只想要一个。
回答by Ravi Shankar
To find all valid address list this is the code I have used
要查找所有有效地址列表,这是我使用的代码
public static IEnumerable<string> GetAddresses()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
return (from ip in host.AddressList where ip.AddressFamily == AddressFamily.lo select ip.ToString()).ToList();
}
回答by Ronald van Zoelen
public static string GetIPAddress(string hostname)
{
IPHostEntry host;
host = Dns.GetHostEntry(hostname);
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
//System.Diagnostics.Debug.WriteLine("LocalIPadress: " + ip);
return ip.ToString();
}
}
return string.Empty;
}
回答by not nor
IPv6
IPv6
lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()
lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()
IPv4
IPv4
lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(1).ToString()
lblIP.Text = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(1).ToString()
回答by Milan ?vec
IPHostEntry ipHostInfo = Dns.GetHostEntry(serverName);
IPAddress ipAddress = ipHostInfo.AddressList
.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);