Help with gethostbyname

Discussion in 'C' started by en_7123, Mar 26, 2010.

  1. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    Here's a simple program that uses gethostbyname

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<error.h>
    #include<strings.h>
    #include<unistd.h>
    #include<arpa/inet.h>
    #include<netdb.h>         //struct hostent
    
    int main()
    
    {
    char s[INET_ADDRSTRLEN];    //Buffer to store the ip
    char **ptr;                  
    char str[100];       //Name of host on which lookup has to be performed
    struct hostent *hptr;                   
    printf("Enter the string :");//Enter the name of host
    gets(str);
    printf("\n");
    if((hptr=gethostbyname(str))==NULL) //Get host information
    {
    printf("Error in host name :");      //Error message 
    exit(1);
    }
    printf("host officail name %s",hptr->h_name); //Print host officail name
    printf("\n\n\n");
    for(ptr=hptr->h_aliases;*ptr!=NULL;ptr++)
    {
    printf("Aliases : %s\n",*ptr); //Print Alias name of the host
    }
    printf("\n");
    for(ptr=hptr->h_addr_list;*ptr!=NULL;ptr++)  //Print all ip address use by host
    {
    printf("Adress list %s\n",inet_ntop(AF_INET,ptr,s,sizeof(s)));
    }
    
    
    
    }
    
    
    Here's the output
    ----------------------------------------------
    ./a.out
    Enter the string :yahoo.com

    host officail name yahoo.com



    Adress list 8.65.120.8
    Adress list 16.65.120.8
    Adress list 24.65.120.8
    Adress list 32.65.120.8
    Adress list 40.65.120.8
    --------------------------------------------------------------------
    ./a.out
    Enter the string :www.yahoo.com

    host officail name any-fp.wa1.b.yahoo.com


    Aliases : www.yahoo.com
    Aliases : fp.wg1.b.yahoo.com

    Adress list 96.145.50.8
    Adress list 104.145.50.8
    -------------------------------------------------------------------------------
    I just want to know why the result differ when i give yahoo.com and www.yahoo.com.Also every time I run it even though giving the same site as input i get a list of different IP address list.Why is so.
    thanx
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Because Yahoo has multiple servers using different CName for many aliases and www is different alias,.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice