在C#中使用Dns.GetHostEntry获取服务器ip [英] Getting server ip using Dns.GetHostEntry in c#
问题描述
我使用.net 3.5/c#
I use .net 3.5/c#
我需要获取服务器的IP,我正在使用以下代码:
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;
但是,切换到Windows 2008 Server之后,我的IP具有字母格式,而不是通常的格式(数字).MSDN没有对此进行任何说明.有什么想法我应该改变以获得服务器IP吗?谢谢
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
推荐答案
您是否查看了hostInfo.AddressList返回的所有地址?
Did you looked at all the addresses that are returned by hostInfo.AddressList?
当我在LinqPad中执行以下操作时:
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会返回我完整的网络接口列表.就您而言,我认为第一个条目指向IPV6地址
IT will return me a whole list of network interfaces. In your case, I think the first entry points to an IPV6 address
这篇关于在C#中使用Dns.GetHostEntry获取服务器ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!