[英]Purpose of Linked list in struct addrinfo{} in socket programming
I am reading Beejs' Guide to Network Programming 我正在阅读Beejs的网络编程指南
I am facing difficulty in understanding the purpose of the linkedlist ie the final parameter in this structure: 我在理解链表的目的(即此结构中的最终参数)时遇到困难:
struct addrinfo {
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname
struct addrinfo *ai_next; // linked list, next node
};
What is the need of this ? 这有什么需要? next node means the next client or what?
下一个节点意味着下一个客户端还是什么?
A host can have more that one IP address. 一台主机可以有多个IP地址。 For example an IPv4 and an IPv6 address, or multiple IPv4 addresses.
例如,一个IPv4和一个IPv6地址,或多个IPv4地址。 Therefore
getaddrinfo()
gives you a pointer to a linked list of one or more addrinfo
structures, and ai_next
is the pointer to the next element, or NULL
for the last element in the list. 因此,
getaddrinfo()
为您提供一个指向一个或多个addrinfo
结构的链接列表的指针,而ai_next
是指向下一个元素的指针,或者为列表中最后一个元素的NULL
。
Example (print all IP addresses for a host): 示例(打印主机的所有IP地址):
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *addrs, *addr;
getaddrinfo("www.google.com", NULL, &hints, &addrs);
// addrs points to first addrinfo structure.
// Traverse the linked list:
for (addr = addrs; addr != NULL; addr = addr->ai_next) {
char host[NI_MAXHOST];
getnameinfo(addr->ai_addr, addr->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
printf("%s\n", host);
}
freeaddrinfo(addrs);
(Error checking omitted for brevity.) (为简便起见,省略了错误检查。)
evert addrinfo专门用于与节点和服务匹配的每个网络地址,它们通过ai_next相互链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.