您的位置:首页 > 运维架构 > Linux

linux 环境下 用gethostbyname函数获取 DNS的服务器列表 名称 IP

2007-06-07 13:57 706 查看
#include <stdio.h>

#include <netdb.h>//gethostbyname
//#include <cygwin/in.h>//struct in_addr(<linux/in.h>)
#include <netinet/in.h>

void getHostName()
{
struct hostent *myhost;
char ** pp;
struct in_addr addr;
myhost = gethostbyname("www.sohu.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.sohu.com
6B1508 is www.sohu.com
6B1518 is d7.a.sohu.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: