Home > Software design >  UDP header is 16 bit but there's actually 24bits
UDP header is 16 bit but there's actually 24bits

Time:03-22

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!

CodePudding user response:

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.

  • Related