为什么dns.gethotentry()方法返回地址列表为空? [英] why dns.gethotentry() method return addresslist as empty?

查看:419
本文介绍了为什么dns.gethotentry()方法返回地址列表为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码获取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屋!

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