#include <netdb.h>
struct hostent *gethostbyname(const char *name);
struct hostent {
char *h_name; /* official name of host 主机的官方名*/
char **h_aliases; /* alias list 主机备选名称,以NULL结尾的列表*/
int h_addrtype; /* host address type 返回的地址类型,只能是 AF_INET 或 AF_INET6 两种类型*/
int h_length; /* length of address 地址长度(以字节为单位)*/
char **h_addr_list; /* list of addresses (主机的网络地址的指针数组,以NULL结尾的列表)*/
}
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
extern int h_errno;
int main (int argc, char **argv)
{
int i;
struct hostent *hostp;
if (argc < 2) {
printf ("Usage: ./gethostbyname host_name\n");
exit (1);
}
if ((hostp = gethostbyname (argv[1])) == NULL) {
fprintf (stderr, "%s : host '%s'\n", hstrerror (h_errno), argv[1]);
exit (1);
}
printf ("要查询的主机 %s 的信息为:\n", argv[1]);
printf ("主机的官方名:%s\n", hostp->h_name);
printf ("主机的备选名称为:\n");
/*一个主机可能有多个备选名称*/
for (i = 0; hostp->h_aliases[i]; i++)
fprintf (stdout, "主机备选名称%d:%s\n", i, hostp->h_aliases[i]);
printf ("地址类型: %s\n", hostp->h_addrtype == AF_INET? "AF_INET":"AF_INET6");
if (hostp->h_addrtype == AF_INET) {
for (i = 0; hostp->h_addr_list[i]; ++i)
printf ("IP地址: %s\n", inet_ntoa (*(struct in_addr*)hostp->h_addr_list[i]));
}
return (0);
}
./gethostbyname www.sina.com
要查询的主机 www.sina.com 的信息为:
主机的官方名:ara.sina.com.cn
主机的备选名称为:
主机备选名称0:www.sina.com
主机备选名称1:us.sina.com.cn
主机备选名称2:news.sina.com.cn
主机备选名称3:jupiter.sina.com.cn
地址类型: AF_INET
IP地址: 58.63.236.46
IP地址: 58.63.236.47
IP地址: 58.63.236.48
IP地址: 58.63.236.49
IP地址: 58.63.236.50
IP地址: 58.63.236.26
IP地址: 58.63.236.27
IP地址: 58.63.236.28
IP地址: 58.63.236.29
IP地址: 58.63.236.30
IP地址: 58.63.236.31
IP地址: 58.63.236.32
IP地址: 58.63.236.42
IP地址: 58.63.236.43
IP地址: 58.63.236.44
IP地址: 58.63.236.45
NAME TYPE VALUE
--------------------------------------------------
foo.example.com. CNAME bar.example.com.
bar.example.com. A 192.0.2.23
# dig www.baidu.com
; <<>> DiG 9.7.0-P1 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15142
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 90 IN CNAME www.a.shifen.com.
www.a.shifen.com. 136 IN A 220.181.111.148
www.a.shifen.com. 136 IN A 220.181.112.143
;; Query time: 16 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Thu Jun 7 23:03:00 2012
;; MSG SIZE rcvd: 90
C:\\Users\\Administrator>ping www.baidu.com
正在 Ping www.a.shifen.com [220.181.111.148] 具有 32 字节的数据:
来自 220.181.111.148 的回复: 字节=32 时间=37ms TTL=54
来自 220.181.111.148 的回复: 字节=32 时间=37ms TTL=54
来自 220.181.111.148 的回复: 字节=32 时间=36ms TTL=54
来自 220.181.111.148 的回复: 字节=32 时间=37ms TTL=54
220.181.111.148 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 36ms,最长 = 37ms,平均 = 36ms
# ./gethostbyname www.baidu.com
要查询的主机 www.baidu.com 的信息为:
主机的官方名:www.a.shifen.com
主机的备选名称为:
主机备选名称0:www.baidu.com
地址类型: AF_INET
IP地址: 220.181.112.143
IP地址: 220.181.111.148
# ./gethostbyname www.mobaoge.net
要查询的主机 www.mobaoge.net 的信息为:
主机的官方名:common.xgslb.net
主机的备选名称为:
主机备选名称0:www.mobaoge.net
主机备选名称1:www.mobaoge.net.xgslb.net
地址类型: AF_INET
IP地址: 218.75.30.22
IP地址: 113.107.43.238
# dig www.mobaoge.net
; <<>> DiG 9.7.0-P1 <<>> www.mobaoge.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20514
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.mobaoge.net. IN A
;; ANSWER SECTION:
www.mobaoge.net. 27 IN CNAME www.mobaoge.net.xgslb.net.
www.mobaoge.net.xgslb.net. 13 IN CNAME common.xgslb.net.
common.xgslb.net. 461 IN A 218.75.30.22
common.xgslb.net. 461 IN A 59.57.12.38
;; Query time: 6 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Thu Jun 7 23:10:31 2012
;; MSG SIZE rcvd: 122
C:\\Users\\Administrator>ping www.mobaoge.net
正在 Ping common.xgslb.net [59.57.12.38] 具有 32 字节的数据:
来自 59.57.12.38 的回复: 字节=32 时间=26ms TTL=55
来自 59.57.12.38 的回复: 字节=32 时间=26ms TTL=55
来自 59.57.12.38 的回复: 字节=32 时间=25ms TTL=55
来自 59.57.12.38 的回复: 字节=32 时间=26ms TTL=55
59.57.12.38 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 25ms,最长 = 26ms,平均 = 25ms
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |