为什么dns.gethotentry()方法返回地址列表为空? [英] why dns.gethotentry() method return addresslist as empty?
问题描述
我使用以下代码获取IP地址:
var ip = Dns.GetHostEntry(host);
var ipaddress = ip.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();
我可以从命令提示符ping此主机名。
但是当我使用 GethostEntry()
时,会正确返回主机名。
,但$ code> AddressList 为空。
而且它不工作,而我正在给本地机器ipaddress。
为什么会发生?
<尝试使用以下代码:
PHostEntry主机;使用此代码的div class =h2_lin>解决方案
string localIP =;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach(IPAddress ip in host.AddressList)
{
if(ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
返回localIP;
I use the following code to get the IP address:
var ip = Dns.GetHostEntry(host);
var ipaddress = ip.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();
i can able to ping this hostname from command prompt.
but when i use GethostEntry()
, it return the hostname correctly.
but the AddressList
is empty.
And also it does not working, while i am giving local machine ipaddress.
why its happen?
try using using this code:
PHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
这篇关于为什么dns.gethotentry()方法返回地址列表为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!