English 中文(简体)
Problem with System.Net.Dns.GetHostEntry(dnsServer) on .NET 4.0
原标题:

I have been using the following code for months (without problem) in a .NET 2.0/3.5 environment:

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);

Here, server is hardcoded to an IP address, but in my application it could potentially be something like "server.test.com".

When converting my project to .NET 4.0, this code stopped working when directly passing an IP address (still works with a hostname). It crashes with this exception:

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)

Because all I need is the resulting IPEndPoint, I can work around the issue by using IPAddress.Parse to generate the IPAddress object, but I want to know if any of you know why this behaviour changed in .NET 4.0? (If we can t resolve the hostname from the IP address, an exception is now thrown).

最佳回答

Microsoft answered this 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

问题回答

暂无回答




相关问题
How do I build a DNS Query record in Erlang?

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively ...

powerdns-on-rails ArgumentError

My Environments: CentOS 5 ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux] Ruby Enterprise Edition 20090610 passneger or webrick I use this gem list. *** LOCAL GEMS *** actionmailer (2.3.2, 2....

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

Checking if a domain name is registered

How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn t seem to work as well as I had hoped. Is there a solution in ...

How do I setup a reverse DNS entry in Plesk 9.2.2 frontent?

I need to get a reverse DNS entry into my zones file for one of my domains hosted under Plesk 9.2.2. Does anybody have ANY idea how this done? If all else fails I will have to update the zones file ...

热门标签