gpt4 book ai didi

c - 为什么sin_addr在in_addr结构里面?

转载 作者:太空狗 更新时间:2023-10-29 16:18:16 26 4
gpt4 key购买 nike

我的疑问与 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
};

这里的成员 sin_addrstruct in_addr 类型。

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

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

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

最佳答案

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;

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

关于c - 为什么sin_addr在in_addr结构里面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com