Go4Expert

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

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

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 00:25.