[英]What is the need of separate address structure in sockaddr_in?
This is the internet(IPv4) socket address structure defined in netinet/in.h 这是netinet / in.h中定义的Internet(IPv4)套接字地址结构
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr {
in_addr_t s_addr;
};
Here what is the need of separate structure only for address field. 在这里,仅地址字段需要单独的结构。
Why can't we use following structure ? 为什么我们不能使用以下结构?
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
in_addr_t sin_addr;
char sin_zero[8];
};
It's for historical reasons. 这是出于历史原因。 In the early days of socket programming,
struct in_addr
contained a union
of various structures so you could get to the individual bytes. 在套接字编程的早期,
struct in_addr
包含各种结构的union
,因此您可以获取各个字节。 This union
became unnecessary when subnetting and classless addressing came along, but switching out the struct
for a simple unsigned long
would break a lot of code, so it just stayed that way. 当进行子网划分和无类寻址时,这种
union
就变得不必要了,但是将struct
转换为简单的unsigned long
代码会破坏很多代码,因此它保持这种状态。
If you're interested in network programming and you haven't yet picked up a copy of UNIX Network Programming then I'd highly recommend doing so, it's a goldmine for little details like this. 如果您对网络编程感兴趣,但是还没有拿起UNIX网络编程的副本,那么我强烈建议您这样做,它是像这样的小细节的金矿。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.