1

I'm wondering if its possible to get the equivalent of nslookup in C# code?

The biggest constraint is that if the host does not exist, it must return quickly, without an excessive timeout (just like nslookup).

Unfortunately, all of the standard C# calls such as Dns.GetHostEntry() and Ping() suffer from an excessive timeout if the host does not exist, which unfortunately rules them out for the purposes of this question.

Contango
  • 76,540
  • 58
  • 260
  • 305
  • 2
    does this solve your problem? http://stackoverflow.com/questions/5468348/how-to-perform-nslookup-host-server – Ehsan Jan 06 '14 at 10:00
  • Try using Dns.GetHostEntry() – Stephan Zaria Jan 06 '14 at 10:01
  • @Stephan Zaria This call would work, but unfortunately it suffers from a timeout measured in seconds, which rules it of the spec. I've edited the question to correct it. Thanks anyway! – Contango Jan 06 '14 at 10:04
  • 1
    @No One This looks great! I'll try it out now. – Contango Jan 06 '14 at 10:06
  • 1
    The problem is that it takes time to determine that the host does not exist. It cannot return "instantly". You can only use a lower timeout value. –  Jan 06 '14 at 10:07
  • 1
    The basic problem is that DNS is not always consistent. In other words, if you ask your router's DNS, it doesn't have all the (current) IP addresses of all the domain names, so it has to ask a different server. And those take some time to update too, and they might have you wait while there's a different lookup going on, etc. So your only option is to limit the timeout, which you can do through neither the .NET nor WinAPI functions, which means you have to use a custom DNS library which allows you to limit the timeout. – Luaan Jan 06 '14 at 10:21

0 Answers0