UDP 标头是 16 位,但实际上有 24 位
我希望了解 UDP 标头,我发现它实际上是 24 位,根据
struct sockaddr_in {
short sin_family; // e.g. AF_INET //4 bytes
unsigned short sin_port; // e.g. htons(3490) //4 bytes
struct in_addr sin_addr; // see struct in_addr, below //8 bytes
char sin_zero[8]; // zero this if you want to //8 bytes
};
struct in_addr {
unsigned long s_addr; // load with inet_aton()
};
这个解释是16字节。由于 sin_zero[8] 在 16 字节的任何地方都没有使用? UDP HEADER 结构大小仍然是 24 字节。我错过了什么吗?
谢谢!
I am looking to understand UDP header and I see that it's actually 24 bits seen as
struct sockaddr_in {
short sin_family; // e.g. AF_INET //4 bytes
unsigned short sin_port; // e.g. htons(3490) //4 bytes
struct in_addr sin_addr; // see struct in_addr, below //8 bytes
char sin_zero[8]; // zero this if you want to //8 bytes
};
struct in_addr {
unsigned long s_addr; // load with inet_aton()
};
According to this explanation it's 16 bytes. Since sin_zero[8] isn't used anywhere it is 16 bytes ? UDP HEADER The struct size is still 24 bytes. Am I missing something ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是用于表达套接字地址的 C 结构。
它与实际在网络上发送的 UDP 标头的内容不同。
基本上,IPv4 标头是 20 个字节,而其之上的 UDP 标头是 8 个字节,正如您问题中的 Geeks for Geeks 参考文献中所解释的那样。
我建议查看 https://en.wikipedia.org/wiki/User_Datagram_Protocol,或安装Wireshark 并捕获 UDP 数据包以查看其外观。
What you have in your question are the C structures for expressing a socket address.
It is a different animal than what actually gets sent on the wire for a UDP header.
Basically the IPv4 header is 20 bytes and the UDP header on top of that is 8 bytes as is also explained by the Geeks for Geeks reference in your question.
I recommend looking at https://en.wikipedia.org/wiki/User_Datagram_Protocol, or installing Wireshark and capturing a UDP packet to see how it looks like.