我也有这种困惑,我做了一些研究。
引自Linux 编程接口
int getaddrinfo(const char * host , const char * service ,
const struct addrinfo * hints , struct addrinfo ** result );
结果参数返回结构列表,而不是单个结构,因为主机和服务可能有多种组合,对应于 、 和 中指定host
的service
条件
hints
。例如,可以为具有多个网络接口的主机返回多个地址结构。
我用自己的域进行了测试copyqwer.com
DNS设置:
Type Name Value TTL
A @ 23.106.150.74 600 seconds
A @ 111.222.17.173 600 seconds
并getaddrinfo
返回具有这 2 个不同 ip 地址的 addr 结构。
一个完整的例子。https://onlinegdb.com/ry1cdoAKr
我的电脑上的输出(我不知道为什么UDP
我的本地电脑上没有输出,但这没关系)
/home/*****/CLionProjects/APUE/cmake-build-debug/APUE copyqwer.com http
flags canon family inet type stream protocol TCP
host copyqwer.com address 111.222.17.173 port 80
flags canon family inet type stream protocol TCP
host - address 23.106.150.74 port 80