Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help with gethostbyname (http://www.go4expert.com/forums/help-with-gethostbyname-t21534/)

en_7123 26Mar2010 13:38

Help with gethostbyname
Here's a simple program that uses gethostbyname


#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
if((hptr=gethostbyname(str))==NULL) //Get host information
printf("Error in host name :");      //Error message
printf("host officail name %s",hptr->h_name); //Print host officail name
printf("Aliases : %s\n",*ptr); //Print Alias name of the host
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
Enter the string :yahoo.com

host officail name yahoo.com

Adress list
Adress list
Adress list
Adress list
Adress list
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
Adress list
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.

shabbir 26Mar2010 16:16

Re: Help with gethostbyname
Because Yahoo has multiple servers using different CName for many aliases and www is different alias,.

All times are GMT +5.5. The time now is 21:42.