#include
#include //gethostbyname
//#include //struct in_addr()
#include
void getHostName()
{
struct hostent *myhost;
char ** pp;
struct in_addr addr;
myhost = gethostbyname("www.linuxdiyf.com");
printf("host name is %s\n",myhost->h_name);
// pp = myhost->h_aliases;
// while(*pp!=NULL)
// {
// printf("%s\n",*pp);
// pp++;
// }
for (pp = myhost->h_aliases;*pp!=NULL;pp++)
printf("%02X is %s\n",*pp,*pp);
pp = myhost->h_addr_list;
while(*pp!=NULL)
{
addr.s_addr = *((unsigned int *)*pp);
printf("address is %s\n",inet_ntoa(addr));
pp++;
}
}
结果:
host name is cachesh1.a.linuxdiyf.com
6B1508 is www.linuxdiyf.com
6B1518 is d7.a.linuxdiyf.com
address is 61.152.234.72
address is 61.152.234.73
address is 61.152.234.75
address is 61.152.234.76
address is 61.152.234.77
address is 61.152.234.71
Linux学习网收集整理 ,(如果您觉得本站不错,请告诉身边的朋友,或转载到论坛、百度知道、贴吧等,记得带网址哟,不胜感激!)。
#include //gethostbyname
//#include //struct in_addr()
#include
void getHostName()
{
struct hostent *myhost;
char ** pp;
struct in_addr addr;
myhost = gethostbyname("www.linuxdiyf.com");
printf("host name is %s\n",myhost->h_name);
// pp = myhost->h_aliases;
// while(*pp!=NULL)
// {
// printf("%s\n",*pp);
// pp++;
// }
for (pp = myhost->h_aliases;*pp!=NULL;pp++)
printf("%02X is %s\n",*pp,*pp);
pp = myhost->h_addr_list;
while(*pp!=NULL)
{
addr.s_addr = *((unsigned int *)*pp);
printf("address is %s\n",inet_ntoa(addr));
pp++;
}
}
结果:
host name is cachesh1.a.linuxdiyf.com
6B1508 is www.linuxdiyf.com
6B1518 is d7.a.linuxdiyf.com
address is 61.152.234.72
address is 61.152.234.73
address is 61.152.234.75
address is 61.152.234.76
address is 61.152.234.77
address is 61.152.234.71