UDP is a transport layer protocol that is unreliable and connectionless. UDP has few additional features beyond reuse/decomposition and a small amount of error detection, so writing applications using UDP is like dealing with the IP layer.

UDP protocol Format

UDP protocol format is simple, only provides 64-bit (8 bytes), its structure is as follows:

As you can see from the figure, the UDP header has only four fields, each of which is two bytes. And all four fields are easier to understand. It is worth noting that the length field specifies the length (in bytes) of the UDP packet segment, including the header. For example, if the data sent at the application layer is 100 bytes, the length field in a UDP packet should be 108 (100+8 bytes header). UDP verification and error detection are provided. Because UDP needs to ensure that the data transmitted is correct, a link may not use the error detection protocol during transmission. In this case, if the data is sent incorrectly, problems may occur. So UDP must provide error detection at the transport layer on an end-to-end basis.

UDP verification and the principle is that all 16-bit words of the sender are calculated and the sum of any overflow encountered is rolled back. The resulting result is then placed in the verification and field of the UDP packet segment. Let’s start with an example. Suppose there are three 16bit words:

0110 0110 0110 0000 0101 0101 0101 1000 1111 0000 1100

The sum of the first two of these 16-bit words is:

0110 0110 0110 0000 0101 0101 0101 1011 1011 1011 0101

Add the sum above and the third word, get

1011 1011 1011 0101 1000 1111 0000 1100 0100 1010 1100 0010 Note that the last addition has overflow, it is to be rolled back. Inverse is replacing all the zeros with ones, all the ones with zeros. So the inverse of this sum, 0100, 1010, 1100, 0010, is 1011, 0101, 0011, 1101, which becomes a checksum.

Error detection practice

Now take a look at the details of a UDP message, here I wrote a simple UDP program, and then send a client connection, send a Hello to send to the server. The packet is captured and analyzed using the Wireshark.

The following data can be obtained through packet capture

Then verify through the verification program, the code is as follows

#include<stdio.h>
unsigned short checksum(unsigned short *buf,int nword)
{
unsigned long sum;
for(sum=0; nword>0; nword--) { sum += *buf++; sum = (sum>>16) + (sum&0xffff);
}
return ~sum;
}
void main(a)
{  
	unsigned short buffer[20] = {0x0aaa.0x3bbf.0xd20e.0x960d.0x0011.0x001c.0xd123.0x2742.0x001c.0x0000.0x6c41.0x5661.0x0000.0x0e00.0xf8b6.0xd401.0x9313.0x0000.0x0000.0x0000};
	int n=20;  
	unsigned short re_checksum;     
	re_checksum=checksum(buffer,n);
	printf("%x\t",re_checksum); 
	if(re_checksum==0x285c) 
		printf("Checksum correct! \n"); 
	else          
		printf("Checksum incorrect! \n");
}

Copy the code

Verify that the output is 73C0. The result is the same as the Checksum field. The Checksum is calculated by adding a false UDP header (source IP address, destination IP address, UDP packet length, 17 bytes). So the overall calculation formula is, pseudo header plus UDP header: SRC IP + DES IP + UDP length + UPd protocol + SRC port + DES port + check sum + UDP length + data length And then if you have an overflow you flip it, and then you take the reverse.

UDP server based on Python

The client

As you can see from the example, UDP is a connectionless protocol, so the encoding is very simple, the server binding IP address, and then can receive data. When sending data, the client directly specifies the IP address and port number, and calls sendto.

As you can see from the example, UDP connections are not saved. So every time you send data, you create a new connection and send data. The overall process is as follows: 1. Register the destination IP address and port number with the UDP socket. 2. Data transmission; 3. Delete the destination address registered in the UDP socket. These three steps are basically repeated every time data is sent. The connect function allows UDP to connect to the server and maintain the connection continuously, which can optimize performance.

UDP Application Scenarios

UDP is an unreliable connection, but it provides great scalability for the application layer. So for some video software can use UDP, including now Google’s QUIC protocol is also based on UDP.

Reference article: blog.csdn.net/stone_Yu/ar… www.faqs.org/rfcs/rfc107… www.cs.binghamton.edu/~steflik/cs… “Computer networking – top down.” 6th edition