linux下用这个结构体来存放主机信息,
The hostent structure is defined in <netdb.h> as follows:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
*/
#include <stdio.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
struct hostent * hostmine;
struct hostent * hostother;
int i=0;
char buf[16];
hostmine= gethostent();
hostother=gethostbyname("www.hebtu.edu.cn");
if(hostmine == NULL){printf("gethostent error\n");exit(-1);}
if(hostother == NULL){printf("gethostbyname error\n");exit(-1);}
printf("my information*****************************************\n");
printf("h_name\t:%s\n",hostmine->h_name);
while(hostmine->h_aliases[i] !=NULL)
{printf("h_aliases[%d]\t:%s\n",i,hostmine->h_aliases[i]);i++;}
printf("the address types is %d\n",hostmine->h_addrtype);
printf("the ip length is %d\n",hostmine->h_length);
i=0;
while(hostmine->h_addr_list[i]!=NULL){
inet_ntop(AF_INET,hostmine->h_addr_list[i],buf,sizeof(buf));
printf("h_addr_list[%d]:\t%s\n",i,buf);i++;
}
printf("hebtu's information************************************\n");
printf("h_name\t:%s\n",hostother->h_name);
while(hostother->h_aliases[i] !=NULL)
{printf("h_aliases[%d]\t:%s\n",i,hostother->h_aliases[i]);i++;}
printf("the address types is %d\n",hostother->h_addrtype);
printf("the ip length is %d\n",hostother->h_length);
i=0;
while(hostother->h_addr_list[i]!=NULL){
inet_ntop(AF_INET,hostother->h_addr_list[i],buf,sizeof(buf));
printf("h_addr_list[%d]:\t%s\n",i,buf);i++;
}
/*注意主机的地址是以二进制的形式存放在h_addr_list下,要用inet_ntop准换过来
*/
}