struct hostent{
char *h_name; /* official domain name of host */
char **h_aliases; /* null-terminated array of domain names */
int h_addrtype; /* host address type (AF_INET) */
int h_length; /* length of an address, in bytes */
char **h_addr_list; /* null-terminated array of in_addr structs */
};
#define h_addr h_addr_list[0]
这里是这个数据结构的详细资料:
char *h_name : 表示的是主机的规范名。例如 www.google.com 的规范名其实是 www.l.google.com 。
char **h_aliases: 表示的是主机的别名。 www.google.com 就是 google 他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
int h_addrtype : 表示的是主机 ip 地址的类型,到底是 ipv4(AF_INET) ,还是 ipv6(AF_INET6)
int h_length : 表示的是主机 ip 地址的长度
int **h_addr_lisst: 表示的是主机的 ip 地址,注意,这个是以网络字节序存储的。千万不要直接用 printf 带 %s 参数来打这个东西,会有问题的哇。所以到真正需要打印出这个 IP 的话,需要调用 inet_ntop() 。
gethostname() 和gethostbyname()都是返回 hostent 结构体,然后根据返回的 hostent ,查看要查找的内容!!