C语言 为什么sin_addr 在in_addr 结构中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13979150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:49:02  来源:igfitidea点击:

Why is sin_addr inside the structure in_addr?

csocketsunix

提问by Deepankar Bajpeyi

My doubt is related to the following structure of sockets in UNIX :

我的疑问与 UNIX 中套接字的以下结构有关:

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

Here the member sin_addris of type struct in_addr.

这里的成员sin_addr类型为struct in_addr

But I don't get why someone would like to do that as all struct inaddrhas is :

但我不明白为什么有人想这样做,因为所有的struct inaddr都是:

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};

All in_addrhas is just one member s_addr. Why cannot we have something like this :

所有的in_addr都只是一名成员s_addr。为什么我们不能有这样的东西:

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    unsigned long    s_addr ; 
    char             sin_zero[8];  // zero this if you want to
};

采纳答案by Carl Norum

struct in_addris sometimes very different than that, depending on what system you're on. On Windowsfor example:

struct in_addr有时与此非常不同,具体取决于您使用的系统。例如在Windows上:

typedef struct in_addr {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

The only requirementis that it contain a member s_addr.

唯一的要求是它包含一个成员s_addr

回答by Yu Hao

struct in_addris a more than just an integer is because it might have more than in_addr_t. In many systems, it has a union, and the reason of such implementation is for class A/B/C addresses, which are not used now.

struct in_addr不仅仅是一个整数是因为它可能有多个in_addr_t。在很多系统中,它都有一个union,而这样实现的原因是为了A/B/C 类地址,现在没有使用。

Unix Network Programming Volume 1explains the historical reason in detail:

Unix网络编程第1卷详细解释了历史原因:

The reason the sin_addrmember is a structure, and not just an in_addr_t, is historical. Earlier releases (4.2BSD) defined the in_addrstructure as a unionof various structures, to allow access to each of the 4 bytes and to both of the 16-bit values contained within the 32-bit IPv4 address. This was used with class A, B, and C addresses to fetch the appropriate bytes of the address. But with the advent of subnetting and then the disappearance of the various address classes with classless addressing, the need for the union disappeared. Most systems today have done away with the unionand just define in_addras a structure with a single in_addr_tmember.

究其原因,sin_addr成员是一个结构,而不仅仅是一个in_addr_t,是历史。早期版本 (4.2BSD) 将该in_addr结构 定义union为各种结构的 a,以允许访问 4 个字节中的每一个以及 32 位 IPv4 地址中包含的两个 16 位值。这与 A、B 和 C 类地址一起使用以获取地址的适当字节。但是随着子网划分的出现以及各种具有无类寻址的地址类的消失,对联合的需求也消失了。今天的大多数系统都取消了 ,union而只是将其定义in_addr为具有单个in_addr_t成员的结构。

回答by ouah

Because the in_addrstructure may contain more than one member.

因为in_addr结构可能包含多个成员。

http://pubs.opengroup.org/onlinepubs/009604599/basedefs/netinet/in.h.html

http://pubs.opengroup.org/onlinepubs/009604599/basedefs/netinet/in.h.html