1#include <netdb.h>2#include <string.h>3#include <stdio.h>4#include <arpa/inet.h>5 6 intMainintargcChar*argv[])7 {8 Charhost[]="www.baidu.com";9 structHostent *ht=NULL;Tenht=gethostbyname (host);//Note that the gethostbyname function will replace the previous content if it is used multiple times . One intI=0; A if(HT) - { -printf"Get the host:%s\n", host); theprintf"name:%s\n",ht->h_name); -printf"type:%s\n", ht->h_addrtype==af_inet?"af_inet":"Af_inet6"); -printf"length:%d\n",ht->h_length); - for(i=0;; i++) + { - if(ht->h_addr_list[i]!=NULL) + { Aprintf"ip:%s\n", Inet_ntoa (* (structIN_ADDR *) ht->h_addr_list[i])); /mainly this sentence is more difficult to understand because the INET_NTOA function is dealing with (struct in_addr inaddr) at } - Else - { - Break; - } - } in for(i=0;; i++) - { to if(ht->h_aliases[i]!=NULL) + { -printf"alias%d:%s\n",i,ht->h_aliases[i]); the } * Else $ {Panax Notoginseng Break; - } the } + } A return 0; the}
Program Run Result:
1 Get The host:www.baidu.com 2 name:www.a.shifen.com 3 type:af_inet 4 Length:45 IP:115.239. 211.112 6 IP:115.239. 210.27 7 0: www.baidu.com
1 Ping www.baidu.com2PING www.a.shifen.com (115.239.210.27) About( -) bytes of data.3 -bytes from 115.239.210.27: icmp_req=1Ttl= -Time=34.9Ms4 -bytes from 115.239.210.27: icmp_req=2Ttl= -Time=34.5Ms5^C64 bytes from 115.239.210.27: icmp_req=3Ttl= -Time=33.5Ms6 7---www.a.shifen.com ping statistics---8 3Packets Transmitted,3Received0%packet loss, time 10122ms9RTT Min/avg/max/mdev =33.547/34.347/34.977/0.633Ms
DNS Domain name resolution