【问题标题】:Getting server ip using Dns.GetHostEntry in c#在 C# 中使用 Dns.GetHostEntry 获取服务器 ip
【发布时间】:2010-06-21 23:02:29
【问题描述】:

我使用 .net 3.5/c#

我需要获取服务器的 IP,我使用的是这段代码:

string strHostName = Dns.GetHostName();
IPHostEntry hostInfo = Dns.GetHostEntry(strHostName);
string serverIpParam = hostInfo.AddressList[0].ToString();


return serverIpParam;

但是,在切换到 Windows 2008 服务器后,我的 IP 具有字母格式而不是通常的格式(数字)。 MSDN 对此没有任何说明。 任何想法我应该改变以获得服务器IP? 谢谢

【问题讨论】:

  • 您是否查看过 AddressList 数组还包含什么?

标签: dns


【解决方案1】:

您是否查看了 hostInfo.AddressList 返回的所有地址?

当我在 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 地址

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2022-01-10
    • 1970-01-01
    • 2017-05-06
    • 2016-12-07
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多