Getting server ip using Dns.GetHostEntry in c#

牧云@^-^@ 提交于 2019-12-14 03:08:18

问题


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

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