码迷,mamicode.com
首页 > 其他好文 > 详细

DNS域名解析

时间:2016-02-15 22:45:22      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<netdb.h>
 2 #include<string.h>
 3 #include<stdio.h>
 4 #include<arpa/inet.h>
 5 
 6 int main(int argc,char *argv[])
 7 {
 8     char host[]="www.baidu.com";
 9     struct hostent *ht=NULL;
10     ht=gethostbyname(host);//注意gethostbyname函数如果多次使用的话后面的内容会代替前面的内容的
11     int i=0;
12     if(ht)
13     {
14         printf("get the host:%s\n",host);
15         printf("name:%s\n",ht->h_name);
16         printf("type:%s\n",ht->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
17         printf("length:%d\n",ht->h_length);
18         for(i=0;;i++)
19         {
20             if(ht->h_addr_list[i]!=NULL)
21             {
22                 printf("IP:%s\n",inet_ntoa(*((struct in_addr *)ht->h_addr_list[i])));//主要是这一句比较难理解    因为inet_ntoa函数所处理的是(struct in_addr inaddr)
23             }
24             else
25             {
26                 break;
27             }
28         }
29         for(i=0;;i++)
30         {
31             if(ht->h_aliases[i]!=NULL)
32             {    
33                 printf("alias %d:%s\n",i,ht->h_aliases[i]);
34             }
35             else
36             {
37                 break;
38             }
39         }
40     }
41         return 0;
42 }

程序运行结果:

1 get the host:www.baidu.com
2 name:www.a.shifen.com
3 type:AF_INET
4 length:4
5 IP:115.239.211.112
6 IP:115.239.210.27
7 alias 0:www.baidu.com
1 ping www.baidu.com
2 PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
3 64 bytes from 115.239.210.27: icmp_req=1 ttl=128 time=34.9 ms
4 64 bytes from 115.239.210.27: icmp_req=2 ttl=128 time=34.5 ms
5 ^C64 bytes from 115.239.210.27: icmp_req=3 ttl=128 time=33.5 ms
6 
7 --- www.a.shifen.com ping statistics ---
8 3 packets transmitted, 3 received, 0% packet loss, time 10122ms
9 rtt min/avg/max/mdev = 33.547/34.347/34.977/0.633 ms

 

DNS域名解析

标签:

原文地址:http://www.cnblogs.com/wireless-dragon/p/5191354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!