获取主机名、IP——gethostent(),gethostbyname(),gethostbyaddr()

本文详细介绍了获取主机名和IP的函数gethostent(), gethostbyname(), gethostbyaddr()的工作原理,包括查询顺序、返回值特性,以及其在不同系统如Ubuntu中的应用。这些函数利用/etc/hosts、DNS或NIS服务器获取信息,并且gethostbyname*()和gethostbyaddr*()已被getaddrinfo()和getnameinfo()替代。" 103219561,7854780,Spark BlockManager详解,"['大数据开发', 'Spark框架', '内存管理', '存储系统', '分布式计算']
摘要由CSDN通过智能技术生成

1、原理:查询 /etc/hosts 等文件及 DNS or NIS 服务器
The  domain  name  queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a broken out line from /etc/hosts, and the Network Information Service (NIS or YP), depending upon the contents of the order line in /etc/host.conf.  The default  action  is  to  query named(8), followed by /etc/hosts.(ubuntu man page)

The network configuration information returned by these functions can be kept in a number of places. They can be kept in static files (/etc/hosts, /etc/services, etc.), or they can be managed by a name service, such as DNS (Domain Name System) or NIS (Network Information Service). 

2、说明:
1) gethostbyname*() 和 gethostbyaddr*() 已经过时,用 getaddrinfo() 和 getnameinfo() 替代
The  gethostbyname*()  and  gethostbyaddr*()  functions  are  obsolete. Applications should use getaddrinfo(3) and getnameinfo(3) instead.
POSIX.1-2001 specifies gethostbyname(), gethostbyaddr(), sethostent(), endhostent(), gethostent(), and h_errno;
gethostbyname(), gethostbyaddr(), and h_errno are marked  obsolescent  in  that standard.
POSIX.1-2008 removes the specifications of gethostbyname(), gethostbyaddr(), and h_errno, recommending the use of getaddrinfo(3) and getnameinfo(3) instead.
(ubuntu man page)

The gethostbyname and gethostbyaddr functions only support IPv4. The API for resolving IPv6 addresses went through several iterations, as

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值