locked
Dns.GetHostEntry(server) does not work sometimes, however Dns.GetHostByAddress(server) does

    Question

  • Hello, everybody. I have this problem I could managed to resolve, but I will be more than pleased to know what is wrong in my code or everywhere else for that to happen.

    I copied a code for a FTP client library for c# in the .Net FrameWork 3.5. It worked very well most of the time but, in certain installations, in certain times, it stoped working at all. I started logging events in the application until I discovered where the problem was:

    IPHostEntry

     

    ipHost = Dns.GetHostEntry(server);

    That line, sometimes, throws a socket exception with code 11001, meaning no host is known with that "server". I shoud add that, by the time the program tries to do GetHostEntry, the server variable has a valid IP number.
    After reading much documentation in the InterNet I find a comment from a user indicating that sometimes GetHostEntry does not work even GetHostByAddress does. I modified the code in the library so it started using GetHostByAddress instead of GetHostEntry. And it instantly began to work, flawless.

    IPHostEntry ipHost = Dns.GetHostByAddress(server);

    The only problem is that GetHostByAddress is deprecated and encouraged to be nos used in the 3.5 framework. In fact it brings a compiler warning to my application.

    I would want to know what is going on with that. The problem arises with Dynamic DNS resolutions. Some of out customers need to be accessible by a hostname because they have DSL. DSL use dynamic IP addresses so we use no-ip or dyndns for mapping the dynamic IP to a hostname.

    I hope I have given you all the information you need.

    Thanks to everyone in advance.
    Ariel.

    Ariel
    Thursday, April 9, 2009 7:44 PM

Answers

  • This problem is usually caused by a mismatch between the forward-lookup DNS entries and the reverse lookup entries.  GetHostEntry first performs a reverse lookup, which resolves the IP address you pass it to a hostname.  It then performs a forward lookup on this hostname to get a full list of IP addresses for the IPHostEntry.  If the name returned by the reverse lookup does not actually resolve, that exception is thrown and there is no way to discover the result of the reverse lookup.  GetHostByAddress only performs the reverse lookup, and so will "succeed" under the same conditions (but you'll get a failure if you try to use the returned hostname later).

    If I'm understanding your scenario correctly, a reverse lookup is almost certainly not going to return the hostname you want anyways.  The reverse lookup is controlled by a DNS server owned by your customer's ISP, and will probably return something directly related to the dynamic IP address, such as 71.231.138.100.hsd1.wa.comcast.net, rather than the no-ip or dyndns-configured hostname.

    -dave

    Monday, April 13, 2009 11:56 PM
    Moderator

All replies

  • The error can happen if the DNS server does not respond in the allotted in time.  It does not always mean that the name is not known.  I reported the fact that a user application can not tell if the error is caused by an un-responsive DNS or is really not known.  That was a couple of years ago...
    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Saturday, April 11, 2009 4:23 PM
  • This problem is usually caused by a mismatch between the forward-lookup DNS entries and the reverse lookup entries.  GetHostEntry first performs a reverse lookup, which resolves the IP address you pass it to a hostname.  It then performs a forward lookup on this hostname to get a full list of IP addresses for the IPHostEntry.  If the name returned by the reverse lookup does not actually resolve, that exception is thrown and there is no way to discover the result of the reverse lookup.  GetHostByAddress only performs the reverse lookup, and so will "succeed" under the same conditions (but you'll get a failure if you try to use the returned hostname later).

    If I'm understanding your scenario correctly, a reverse lookup is almost certainly not going to return the hostname you want anyways.  The reverse lookup is controlled by a DNS server owned by your customer's ISP, and will probably return something directly related to the dynamic IP address, such as 71.231.138.100.hsd1.wa.comcast.net, rather than the no-ip or dyndns-configured hostname.

    -dave

    Monday, April 13, 2009 11:56 PM
    Moderator