问题
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