gethostbyname Not Working But nslookup is working Fine

Discussion in 'Programming' started by theblogger, Jul 29, 2008.

  1. #1
    Hi All

    I have a small program which uses gethostbyname to resolve a domain on solaris platform . But it is not working.
    When I checked using ethereal I found that dns query was fired and we received a proper response from DNS (with final IP). One strange thing that I noticed from trace was that dns query was sent out around 3 sec after gethostbyname was called.

    Also when I tried to resolve this domain with nslookup on same machine it was working fine. Have anyone seen such problem ?
     
    theblogger, Jul 29, 2008 IP
  2. theblogger

    theblogger Well-Known Member

    Messages:
    384
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    108
    #2
    I got the solution to this problem . Do following

    1. Open /etc/nsswitch.conf
    2. Replace
    hosts: files dns
    With
    hosts: files [NOTFOUND=continue UNAVAIL=continue FOUND=return] dns [NOTFOUND=return UNAVAIL=return TRYAGAIN=return FOUND=return] files

    Not sure how it works. Can any one explain how its work. Old configuration was working on some servers.
     
    theblogger, Jul 31, 2008 IP
  3. theblogger

    theblogger Well-Known Member

    Messages:
    384
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Tip to debug such issue . got it from http://forums12.itrc.hp.com/service...447627+1217835752182+28353475&threadId=581285

    A trick to use for debugging this kind of problem is "nsquery hosts" followed by the hostname you are looking for.

    Example,
    jbox# nsquery hosts www.atl.hp.com

    Using "files [NOTFOUND=continue] dns" for the hosts policy.

    Searching /etc/hosts for www.atl.hp.com
    www.atl.hp.com was NOTFOUND

    Switch configuration: Allows fallback

    Searching dns for www.atl.hp.com
    Hostname: www.atl.hp.com
    Aliases:
    Address: 15.51.240.8
    Switch configuration: Terminates Search
     
    theblogger, Aug 4, 2008 IP