why dns.gethotentry() method return addresslist as empty?

半腔热情 提交于 2020-01-17 03:40:27

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/31676533/why-dns-gethotentry-method-return-addresslist-as-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!