dns - .NET 4.0 上的 System.Net.Dns.GetHostEntry(dnsServer) 问题

标签 dns .net-4.0 socketexception

我已经在 .NET 2.0/3.5 环境中使用以下代码几个月了(没有问题):

string server="192.168.1.3";
IPHostEntry ipe = System.Net.Dns.GetHostEntry(server);
IPAddress ipa = ipe.AddressList[0];
IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);

这里,服务器被硬编码为 IP 地址,但在我的应用程序中,它可能是类似于“server.test.com”的内容。

将我的项目转换为 .NET 4.0 时,此代码在直接传递 IP 地址时停止工作(仍然适用于主机名)。它因以下异常而崩溃:

System.Net.Sockets.SocketException was unhandled
  Message=The requested name is valid, but no data of the requested type was found
  Source=System
  ErrorCode=11004
  NativeErrorCode=11004
  StackTrace:
       at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
       at System.Net.Dns.GetHostEntry(String hostNameOrAddress)

因为我需要的只是生成的 IPEndPoint,所以我可以通过使用 IPAddress.Parse 生成 IPAddress 对象来解决该问题,但我想知道你们中是否有人知道为什么这种行为在 .NET 4.0 中发生了变化? (如果我们无法从 IP 地址解析主机名,则会抛出异常)。

最佳答案

微软回答了这个问题 here :

this was purposely changed to more consistently represent name resolution failures. If you have input strings that you just want to convert to IPAddresses, it is recommended that you use IPAddress.TryParse or Dns.GetHostAddresses

关于dns - .NET 4.0 上的 System.Net.Dns.GetHostEntry(dnsServer) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714449/

相关文章:

nginx - 如何设置 NGINX 来屏蔽 CNAME 记录

azure - 将点到站点与 Azure VPN 网关结合使用时的私有(private) DNS 方法?

entity-framework - 如何在 Entity Framework 中维护有序列表?

c# - System.Net.Sockets.SocketException

ssl - 如何从手持设备为 SSL/TLS 建立安全通道?

azure - Microsoft Azure 自定义域检查自定义域添加操作失败

node.js - 在 Nodejs 上使用 DNS 进行 Consul 服务发现

c# - 访问本地 IIS : "This operation is not supported for a relative URI." 上托管的 WCF 服务时出错

.net-4.0 - 有没有办法知道任务并行库中当前正在运行哪些任务?

ajax - JMeter java.net.SocketException : Socket closed