Can Dns.GetHostEntry ever return an IPHostEntry with an empty AddressList?

前端 未结 4 1476
予麋鹿
予麋鹿 2021-01-15 10:57

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 some

4条回答
  •  一生所求
    2021-01-15 11:44

    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...

提交回复
热议问题