我想了解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字节。我是不是错过了什么?
谢谢!
您的问题中有用于表示套接字地址的C结构。
这是一种不同于实际通过UDP报头发送的动物。
基本上,IPv4报头是20个字节,而UDP报头是8个字节,Geeks for Geeks在您的问题中也解释了这一点。
我建议查看https://en.wikipedia.org/wiki/User_Datagram_Protocol,或者安装Wireshark并捕获UDP数据包以查看其外观。