问题
I use .net 3.5/c#
I need to get server's IP, I was using this code:
string strHostName = Dns.GetHostName();
IPHostEntry hostInfo = Dns.GetHostEntry(strHostName);
string serverIpParam = hostInfo.AddressList[0].ToString();
return serverIpParam;
However, after a switch to Windows 2008 server, my IP has letter format instead of usual format (digits). MSDN doesn't shed any light on this. Any ideas what I should change to get the server IP? Thanks
回答1:
Did you looked at all the addresses that are returned by hostInfo.AddressList?
When I execute the following in LinqPad:
string strHostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostEntry(strHostName);
for(int index=0; index < hostInfo.AddressList.Length; index++) {
Console.WriteLine(hostInfo.AddressList[index]);
}
IT will return me a whole list of network interfaces. In your case, I think the first entry points to an IPV6 address
来源:https://stackoverflow.com/questions/3089091/getting-server-ip-using-dns-gethostentry-in-c-sharp