Sticky package of Java network programming

What is the TCP protocol?

The full name of TCP is Transmission Control Protocol (Transmission Control Protocol), which is defined by IETF's RFC 793 and is a connection-oriented point-to-point transport layer communication protocol. TCP is the most commonly used protocol on the Internet, and it is also the basis for HTTP (HTTP 1.0/HTTP 2.0) communication. When we request a web page in a browser, the computer will send a TCP packet to the address of the Web server, asking it to The web page is returned to us, the web server responds by sending a stream of TCP packets, and the browser stitches these packets together to form the web page.

The whole point of TCP is that it's reliable, it orders packets by numbering them, and it does error checking by having the server send the response back to the browser saying "received", so in transit No data will be lost or destroyed.

UDP

UDP (user datagram protocol, user datagram protocol) is connectionless, message-oriented, and provides high-efficiency services. The block merging optimization algorithm will not be used. Since UDP supports a one-to-many mode, the skbuff (socket buffer) at the receiving end uses a chain structure to record each arriving UDP packet. In each UDP packet There is a message header (message source address, port and other information) in the message, so that it is easy for the receiving end to distinguish and process. That is, message-oriented communication has a message protection boundary.

Since TCP has no message protection boundary, it is necessary to deal with the message boundary problem at the message receiving end, which is what we call sticky packets and unpacking problems; while UDP communication does not need to consider this problem.

What is a sticky bag?

The sticky packet problem means that when two messages are sent, such as ABC and DEF, but the other end receives ABCD, the situation of reading two pieces of data at one time is called sticky packet (normal situation should be are read one by one).

Why is there a sticky package problem?

This is because TCP is a connection-oriented transmission protocol. The data transmitted by TCP is in the form of streams, and stream data has no clear start and end boundaries, so TCP has no way to judge which segment of the stream belongs to a message.

The main reasons for the sticky package:

  • Each time the sender writes data < socket (Socket) buffer size;
  • The receiver reads the socket (Socket) buffer data not in a timely manner.
What is a buffer?

A buffer, also known as a cache, is a part of the memory space. That is to say, a certain storage space is reserved in the memory space, and these storage spaces are used to buffer input or output data, and this part of the reserved space is called a buffer.

The advantages of the buffer take the writing of the file stream as an example. If we do not use the buffer, the CPU will interact with the low-speed storage device, that is, the disk, for each write operation, and the entire writing speed of the file will be limited by the low-speed Storage device (disk). But if the buffer is used, each write operation will first save the data in the high-speed buffer memory, and when the data in the buffer reaches a certain threshold, the file will be written to the disk at one time. Because the writing speed of the memory is much faster than the writing speed of the disk, when there is a buffer, the writing speed of the file is greatly improved.

Sticky package solution

  • The sender and the receiver specify a fixed-size buffer, that is, both sending and receiving use a fixed-size byte[] array length, and use null characters to make up for it when the character length is not enough;
  • Encapsulate a layer of data request protocol on the basis of the TCP protocol, which encapsulates the data packet into the form of data header (storage data body size) + data body, so that the server can know the specific length of each data packet. After knowing the specific boundary of sending data, the problem of half-packet and sticky-packet can be solved;
  • End with a special character, such as "\n", so that we know the end character, thus avoiding the sticky package problem (recommended solution).

Solution 1: Fixed buffer size

The implementation of the fixed buffer size only needs to control the length of the byte (array) sent and received by the server and the client to be the same.

Disadvantages: Although this method can solve the problem of sticky packets, this method of fixed buffer size increases unnecessary data transmission, because this method will use null characters to make up for the small amount of data sent, so this method This method greatly increases the burden of network transmission, so it is not the best solution.

Solution 2: Encapsulate the request protocol

The implementation idea of ​​this solution is to encapsulate the requested data into two parts: data header + data body, store the size of the data body in the data header, and continue to read the data when the read data is smaller than the size in the data header , until the length of the read data is equal to the length in the data header.

Disadvantages: Because this method can get the boundary of the data, it will not cause the problem of sticky packets, but this implementation method has a large coding cost and is not elegant enough, so it is not the best implementation solution.

Solution 3: end of special character, read by line

The core of this solution is to use the BufferedReader and BufferedWriter that comes with Java, that is, the input character stream and the output character stream with a buffer, and add \n to the end when writing, and use readLine when reading Read data by line, so that you know the boundary of the flow, thus solving the problem of sticky packets and half packets.

Summarize

Sticky packet means that two pieces of information have been read. Under normal circumstances, messages should be read one by one. The reason for sticky packets and half packets is that TCP transmission is carried out in the form of streams, and stream data is not clear. The start and end are identified, thus causing the problem.

We provide 3 solutions for sticky packets: the most recommended one is to use BufferedReader and BufferedWriter to read, write and distinguish messages by line.

Je suppose que tu aimes

Origine blog.csdn.net/qq_39756007/article/details/126859877
conseillé
Classement