Dns.GetHostEntry能否返回一个空的AddressList的IPHostEntry? [英] Can Dns.GetHostEntry ever return an IPHostEntry with an empty AddressList?

查看:374
本文介绍了Dns.GetHostEntry能否返回一个空的AddressList的IPHostEntry?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否可以有一个主机名可以成功解决但返回的hostEntry.AddressList为空的情况。



目前我在做如下所示:

  IPHostEntry hostEntry = Dns.GetHostEntry(some.hostname.tld); 
if(hostEntry.AddressList.Count()< 1)
{
//可以发生吗?
抛出新的ArgumentException(hostName没有分配的IP地址);
}
TcpClient client = new TcpClient(hostEntry.AddressList [0],1234);

我的假设是,如果没有找到主机名或者其他的AddressList,Dns.GetHostEntry会抛出异常不空,但我不确定。

解决方案

不,您不会看到空的地址列表:甚至如果您查询存在但没有A或AAAA(IPv6)记录的DNS标签,则将抛出SocketException(No Such Host is Known)。



可以通过查看.NET参考源版本的DNS.cs中的函数 InternalGetHostByName(string hostName,bool includeIPv6)来验证这一点。除了特定于平台的预防措施外,DNS查找是围绕Winsock的简单包装器 gethostbyname 函数。



Gethostbyname将失败或返回地址列表。一个空的地址列表永远不会返回,因为在这种情况下,WSANO_DATA(有效的名称,没有请求的类型的数据记录)的功能将失败,这转换为我们已经在.NET中看到的套接字异常。



编辑2012年5月,由响应提示,表示仍然返回空列表:请注意,此答案仅适用于Win32,而像WinCE这样的平台可能会完全不同如果您在Win32上看到空列表行为,并且您所要求的请求是针对公开的DNS服务器,请发布您的代码...


I'm just wondering if there can be a case where the hostname can be successfully resolved but the returned hostEntry.AddressList is empty.

Currently I'm doing something like this:

IPHostEntry hostEntry = Dns.GetHostEntry("some.hostname.tld");
if (hostEntry.AddressList.Count() < 1)
{
  // can that ever happen?
  throw new ArgumentException("hostName has no assigned IP-Address");
}
TcpClient client = new TcpClient(hostEntry.AddressList[0], 1234);

My assumption is that Dns.GetHostEntry either throws an exception if the hostname is not found or otherwise the AddressList is nonempty, but I'm not sure about that.

解决方案

No, you'll not see an empty address list: even if you query a DNS label that does exist, but has no A or AAAA (IPv6) records, a SocketException ("No Such Host is Known") will be thrown.

You can verify this by looking at the function InternalGetHostByName(string hostName, bool includeIPv6) in DNS.cs from the .NET Reference Source release. With the exception of some platform-specific precautions, DNS lookups are a simple wrapper around the Winsock gethostbyname function.

Gethostbyname will either fail, or return an address list. An empty address list is never returned, because the function will fail with WSANO_DATA ("Valid name, no data record of requested type") in this case, which translates to the socket exception we already saw in .NET.

EDIT May 2012, prompted by responses stating that an empty list is returned anyway: do note that this answer only applies to Win32, and that platforms like WinCE may behave quite differently. If you're seeing 'empty list' behavior on Win32, and the request you're making is against a publicly available DNS server, please post your code...

这篇关于Dns.GetHostEntry能否返回一个空的AddressList的IPHostEntry?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆