struct addrinfo 和 struct sockaddr 有什么区别 [英] What is the difference between struct addrinfo and struct sockaddr

查看:80
本文介绍了struct addrinfo 和 struct sockaddr 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,struct addrinfo 用于准备套接字地址结构,struct sockaddr 包含套接字地址信息.但这实际上意味着什么?struct addrinfo 包含一个指向 struct sockaddr 的指针.为什么要把它们分开?为什么我们不能把 sockaddr 里面的所有东西都合并到 addr_info 中?

From what I understand struct addrinfo is used to prep the socket address structure and struct sockaddr contains socket address information. But what does that actually mean? struct addrinfo contains a pointer to a struct sockaddr. Why keep them separate? Why can't we combine all things within sockaddr into addr_info?

我只是在这里猜测,但它们分离的原因是为了在传递结构时节省空间吗?例如在 bind() 调用中,它所需要的只是端口号和互联网地址.所以这两个都被分组在一个 struct sockaddr 中.那么,我们可以只传递这个小结构体而不是更大的结构体 addrinfo 吗?

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. So both of these are grouped in a struct sockaddr. So, we can just pass this small struct instead of the larger struct 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 addrinfogetaddrinfo() 返回,成功时包含一个链表用于指定主机名和/或服务的此类 struct .

struct addrinfo is returned by getaddrinfo(), and contains, on success, a linked list of such structs for a specified hostname and/or service.

ai_addr 成员实际上并不是一个 struct sockaddr,因为那个 struct 只是一个包含所有通用成员的通用成员其他,用于确定您实际拥有的结构类型.根据您传递给 getaddrinfo() 的内容以及该函数发现的内容,ai_addr 实际上可能是指向 struct sockaddr_in 的指针,或 struct sockaddr_in6 或其他任何内容,具体取决于适合该特定地址条目的内容.这是它们保持分离"的一个很好的原因,因为该成员可能指向一堆不同类型的 struct 中的一个,如果您尝试对所有内容进行硬编码,则无法做到这一点将成员放入struct addrinfo,因为那些不同的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. 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. This is one good reason why they're kept "separate", because that member might point to one of a bunch of different types of structs, which it couldn't do if you tried to hardcode all the members into struct addrinfo, because those different structs have different members.

如果您有主机名,这可能是获取此信息的最简单方法,但这不是唯一方法.对于 IPv4 连接,您可以自己填充 struct sockaddr_in 结构,如果您愿意并且您有数据可以这样做,并且避免经历调用 getaddrinfo(),如果它需要进入互联网为您收集信息,您可能需要等待.您根本不必使用 struct addrinfo.

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. You don't have to use struct addrinfo at all.

这篇关于struct addrinfo 和 struct sockaddr 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆