简体   繁体   English

struct addrinfo和struct sockaddr有什么区别

[英]What is the difference between struct addrinfo and struct sockaddr

From what I understand struct addrinfo is used to prep the socket address structure and struct sockaddr contains socket address information. 根据我的理解,struct addrinfo用于准备套接字地址结构,struct sockaddr包含套接字地址信息。 But what does that actually mean? 但这究竟意味着什么呢? struct addrinfo contains a pointer to a struct sockaddr. struct addrinfo包含指向struct sockaddr的指针。 Why keep them separate? 为什么要将它们分开? Why can't we combine all things within sockaddr into addr_info? 为什么我们不能将sockaddr中的所有内容组合到addr_info中?

I'm just guessing here but is the reason for their separation is to save space when passing structs? 我只是在这里猜测,但他们分离的原因是为了在传递结构时节省空间? For example in the bind() call, all it needs is the port number and the internet address. 例如,在bind()调用中,它需要的只是端口号和Internet地址。 So both of these are grouped in a struct sockaddr. 所以这两个都分组在struct sockaddr中。 So, we can just pass this small struct instead of the larger struct addrinfo? 那么,我们可以传递这个小结构而不是更大的结构addrinfo?

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
};

struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
}; 

struct addrinfo is returned by getaddrinfo() , and contains, on success, a linked list of such struct s for a specified hostname and/or service. struct addrinfogetaddrinfo()返回,并且在成功时包含指定主机名和/或服务的此类struct的链接列表。

The ai_addr member isn't actually a struct sockaddr , because that struct is merely a generic one that contains common members for all the others, and is used in order to determine what type of struct you actually have. ai_addr成员实际上不是struct sockaddr ,因为该struct只是一个包含所有其他struct的通用成员的通用struct ,用于确定实际拥有的结构类型。 Depending upon what you pass to getaddrinfo() , and what that function found out, ai_addr might actually be a pointer to struct sockaddr_in , or struct sockaddr_in6 , or whatever else, depending upon what is appropriate for that particular address entry. 根据您传递给getaddrinfo()以及该函数发现的内容, ai_addr实际上可能是指向struct sockaddr_instruct sockaddr_in6或其他任何内容的指针,具体取决于适合该特定地址条目的内容。 This is one good reason why they're kept "separate", because that member might point to one of a bunch of different types of struct s, which it couldn't do if you tried to hardcode all the members into struct addrinfo , because those different struct s have different members. 这是为什么它们保持“分离”的一个很好的理由,因为该成员可能指向一堆不同类型的struct的一个,如果你试图将所有成员硬编码到struct addrinfo ,它就无法做到,因为那些不同的struct有不同的成员。

This is probably the easiest way to get this information if you have a hostname, but it's not the only way. 如果您有主机名,这可能是获取此信息的最简单方法,但这不是唯一的方法。 For an IPv4 connection, you can just populate a struct sockaddr_in structure yourself, if you want to and you have the data to do so, and avoid going through the rigamarole of calling getaddrinfo() , which you might have to wait for if it needs to go out into the internet to collect the information for you. 对于IPv4连接,您可以自己填充struct sockaddr_in结构,如果您愿意并且您有数据要这样做,并且避免经历调用getaddrinfo()的严格要求,如果需要,您可能需要等待它进入互联网为您收集信息。 You don't have to use struct addrinfo at all. 您根本不必使用struct addrinfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从struct sockaddr生成struct addrinfo? - How to produce struct addrinfo from struct sockaddr? 使用memset将struct sockaddr_in设置为0和'\\ 0'有什么区别? - What would be the difference between setting a struct sockaddr_in to 0 and '\0' using memset? 当`addrinfo :: ai_addrlen`大于`sizeof(struct sockaddr)`时该怎么办? - What to do when `addrinfo::ai_addrlen` is larger than `sizeof(struct sockaddr)`? 在使用之前将结构如sockaddr_in,sockaddr_in6和addrinfo归零时,这是正确的:memset,初始化器还是其中之一? - When zeroing a struct such as sockaddr_in, sockaddr_in6 and addrinfo before use, which is correct: memset, an initializer or either? struct node和struct node *之间的'->'有什么区别? - What is the difference in '->' between struct node and struct node*? Xcode 4 中未声明的结构 addrinfo - struct addrinfo undeclared in Xcode 4 作为结构初始化器,{}和{0}之间有什么区别? - What is the difference between {} and {0} as struct initialisers? struct {...}和struct {union {struct {...}}}之间有什么区别? - What's the difference between struct { … } and struct { union { struct { … } } }? sockaddr,sockaddr_in和sockaddr_in6有什么区别? - What's the difference between sockaddr, sockaddr_in, and sockaddr_in6? 未定义的结构'addrinfo'winsock2 - Undefined struct 'addrinfo' winsock2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM