Chapter 11 User Data News Agreement UDP
UDP is not connected. Basic format: ---- 20 bytes ---- - 8 Bytes ---- ----- n bytes ----- ip header UDP Header Data ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------ Generate a UDP datagram every time you send it.
UDP head structure: --- 16 bits ------ - 16 bits ------ Source Port Dest Port ------------- ----- ---------------- Length Checksum ------------------ ------------------ Data (if any) ----------------------- -------------- UDP length is the byte length of the UDP head and the data field. The minimum is 8 bytes (ie the first 8 bytes, no data). UDP checksum covers UDP headers and data fields. Unlike IP, IP header checks only the IP header. Unlike TCP, the check of UDP is optional. But generally should be added. When calculating the checksum, it is counted (Ushort, Unsigned Short), which is 16 bits. After the UDP plus the data field, it may be an odd bit. At this time, it is necessary to fill the byte of 0 after the UDP data field to calculate the checksum. In addition, when calculating the checksum, TCP and UDP have a pseudo-head, mainly to calculate the convenience of checksum. UDP's pseudo-head format: -- 16 bits ---- -- 16 bits ---- 32 BITS SOURCE Addr -------------- -------------------- 32 BITS DEST ADDR -------- -------- - -------------- 0 17 UDP Length -------- -------- ------ --------- 17 Type code for UDP protocol
If you don't need to modify the content of the UDP header, it is only possible to use the UDP communication function. The following procedure (not the actual source code):
Service-Terminal:
WSAStartup ();
SCK = Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sin.sin_family = af_INet;
Sin.sin_addr.s_un.s_addr = HTONL (INADDR_Any);
Sin.sin_port = HTONS (51888);
Bind (SCK, SIN, SIZEOF (SIN));
While (True)
{
Recvfrom (SCK, BUF, 256, 0, & from, & Flen);
Sendto (...);
Other processing
}
CloseSocket (SCK);
WSACLEANUP ();
Client:
WSAStartup (); SCK = Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sin.sin_family = af_INet;
Sin.sin_addr.s_un.s_addr = inet_addr ("192.168.1.x");
Sin.sin_port = HTONS (51888);
While (True)
{
Sendto (SCK, BUF ....);
Recvfrom (...);
Other processing
}
CloseSocket (SCK);
WSACLEANUP ();
Server and clients are simple frameworks, fill in some custom processing procedures.
I / O is blocked, meaning that when recvfrom () is called, if there is no data in the buffer, the program will continue to receive data. Sendto () is also similar. Using asynchronous I / O can improve efficiency, multithreading can also be used to improve efficiency. However, since this chapter is not to talk about how to use UDP to communicate, it will not continue to play.
The subsequent part of the IP datagram is discussed. Split is generated due to too long the data to be transmitted, which exceeds the MTU (maximum transmission unit) of the transmission link. Not only the source of the data is possible, and each router will be divided into pieces according to the link situation. All slices are reassembled in the receiving end. The fragmentation of the IP layer can be prevented, and it is achieved by setting the "DF" insertable flag of the IP datagram. Such an IP layer is not active when receiving a data that is too long, but returns an error, and reporting data requires prevention. This point can be used to realize the minimum MTU for the field of communication. A larger MTU is set in advance. If you receive an ICMP error message, the MTU continues to be sent until the error message is not received. It is known that the MTU can help the application-side pre-sliced, reduce the loss of a large number of packets, need to retransmit the risk of the entire datagram due to the loss of some shards. The specific operation can be checked by transmitting a data field of different lengths. The MTU is related to the communication host and is also related to the communication link of the way. The MTU of the entire path is subject to the smallest MTU (wooden bucket theory).