UDP 标头是 16 位,但实际上有 24 位

发布于 2025-01-15 18:52:20 字数 696 浏览 4 评论 0原文

我希望了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2025-01-22 18:52:20

您的问题是用于表达套接字地址的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文