I wrote a small test program that used Dns.GetHostAddresses to resolve a host name. If I entered a hostname that didn't exist, it would throw a SocketException and the message would be No such host is known.
I liked this because the error message was small enough to put into my UI, and straight to the point.
Now I literally copied and pasted the code into a somewhat larger project, but this time when I enter the exact same hostname I did in my test project, it throws a SocketException but the message is set to The requested name is valid, but no data of the requested type was found
Both the stack traces are the same in each program, but the first ones error is WSAHOST_NOT_FOUND where the second one is WSANO_DATA.
I could just output my own error message, but I would like to know why it's throwing a different error each time? There are no differences in the code.
Also, it seems that no matter how many times I run each program, they each keep outputting the same error message. It's not alternating or anything.
===========================================================================
Well I figured out what was causing the problem.
I added Pcap.Net as a reference and was including it in my project with using PcapDotNet.Core;
Anyways, after I commented that out, Dns.GetHostAddresses was throwing the correct exception.
If anyone could tell me why its behaving like that, and how to get around it, that would be great.
There's many reasons why a SocketException will occur in GetHostAddresses. In the case of WSA_NODATA, that means there is an entry in DNS; but no IP address (i.e. no A record). You may get a different error if you can't reach the DNS server but the data is cached. Or, you may get a different error if the DNS server is unreachable and the data isn't cached. Etc. Unfortunately GetHostAddresses wrap all those errors in one exception type. If you want to do something different for each type of error, you'll have to check a relevant property in SocketException. If you just want to detect that "it didn't work", then you don't have to check. But, it's clear that SocketException message is different depending on the failure so I wouldn't recommend just using Message. Not to mention, that will get translated on other language versions of Windows--so, it might look strange if the rest of your application is English, except for this message.
Technically it could have one of the following errors: WSANOTINITIALIZED, WSAENETDOWN, WSAHOST_NOT_FOUND, WSATRY_AGAIN, WSANO_RECOVERY, WSANO_DATA, WSAEINPROGRESS, WSAEFAULT and WSAEINTR. Although some are extremely unlikely.
I also think you'll get different messages depending on whether IPv6 or IPv4 is in play.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With