【问题标题】:Empty AddressList in GetHostEntryGetHostEntry 中的空地址列表
【发布时间】:2016-09-29 08:32:47
【问题描述】:

我正在使用以下代码连接到远程主机:

IPHostEntry hostname = Dns.GetHostEntry("172.29.65.33");
IPAddress address = hostname.AddressList[0]; // IndexOutOfRangeException
...

我的问题是为什么AddressList 是空的?主机在,没有SocketException被抛出...

一些细节:客户已将 Windows XP 升级到 8.1,然后所有的麻烦就开始了。

我已阅读 thisthis 主题,但不幸的是它们对解决问题没有用,因此再次询问。

【问题讨论】:

    标签: c# sockets


    【解决方案1】:

    而不是通过Dns解析地址

    IPHostEntry hostname = Dns.GetHostEntry("172.29.65.33");
    IPAddress address = hostname.AddressList[0];
    ...
    IPEndPoint end = new IPEndPoint(address, port);
    

    可以简单解析

    IPEndPoint end = new IPEndPoint(IPAddress.Parse("172.29.65.33"), port);
    

    这将消除与使用Dns相关的所有问题(套接字异常、空地址列表等)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      相关资源
      最近更新 更多