[英]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 addrinfo
由getaddrinfo()
返回,并且在成功时包含指定主机名和/或服务的此类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_in
或struct 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.