getaddrinfo(),gethostbyname(),getnetbyaddr(),getservbyname(),getprotobyname()对比总结

本文详细介绍了网络编程中常用的名称解析函数,包括gethostbyname(), getaddrinfo(), getnetbyaddr(), getservbyname() 和getprotobyname()。这些函数通过查询/etc/hosts文件、DNS服务器及NIS等方式实现域名到IP地址的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. gethostbyname()

原理:查询 /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). (APUE-2e)

见 http://blog.youkuaiyun.com/duyiwuer2009/article/details/7861938 的分析


2. getaddrinfo()

原理:可以确定的是其实现与 gethostbyname() 中涉及的 /etc/hosts 等文件及 DNS 有关,还与 /etc/services 有关(APUE-2e中 ruptime 的例子就可以说明这一点)

见 http://blog.youkuaiyun.com/duyiwuer2009/article/details/7875206 的分析

其它参考资料:

UNIX Network Programming Volume 1, Third Edition 


3. getnetbyaddr()

原理:The getnetent() function reads the next entry from the networks database(/etc/networks)

见 http://blog.youkuaiyun.com/duyiwuer2009/article/details/7861938 的分析


4. getservbyname()

原理: The getservent() function reads the next entry from the services database(/etc/services)

见 http://blog.youkuaiyun.com/duyiwuer2009/article/details/7871939 的分析


5、getprotobyname()

原理:The  getprotoent() function reads the next entry from the protocols database(/etc/protocols)

见 http://blog.youkuaiyun.com/duyiwuer2009/article/details/7872121 的分析


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值