Netty - sticky and semi-pack package (on)

In the network transmission, stick package, and half a pack should be the most common problems, NIO network framework Netty most commonly used as Java, how it is to solve it? Today, let's see.

definition

TCP transmission, the client sends data, the actual data is written to the cache TCP, stick package, and the package also will have a half at this time.

The client to the server sends two messages ABCand DEFservice side of the receiving end how many kinds of situation? There may be a one-time receipt of all the messages ABCDEF, there are three messages may be received AB, CD, EF.

The above mentioned disposable received all messages ABCDEF, similar to the stick package. If the client sends the packet size is smaller than the cache capacity TCP, and TCP buffer can store multiple packages, the client and server communication time may pass multiple packets, this time from the TCP server cache may look read multiple packets, this phenomenon is called 粘包.

The above said later that received three messages AB, , CD, EFsimilar to half a pack. If the client sends a packet size larger than the cache size of TCP, then the packet is divided into a plurality of packets, sent to the server through the Socket times, from the first server receives the data obtained inside the cache, the actual is part of the overall package, this time produced 半包(half a pack not to say that only received half of the all-inclusive, and is said to receive a part of the whole package).

cause

In fact, from the above definition, we can probably know the cause of the.

Stick package main reasons:

  1. Each time data sender <socket (the Socket) write buffer size
  2. Reading a socket receiver (the Socket) not enough time to buffer data

Half a pack of the main reasons:

  1. Each time data is written to the sender> socket (the Socket) buffer size
  2. MTU (Maximum Transmission Unit, the maximum transmission unit) is greater than the data transmission protocol, and therefore must be unpacked

In fact, we can look at the problem from another angle:

  1. From the 收发point of view, is likely to be a plurality of times receiving a transmission, a plurality of transmission may be received.
  2. From the 传输point of view, is likely to occupy a plurality of transmission packets transmitted, transmission may share a plurality of transport packets.

Root cause, in fact,

TCP is a streaming protocol message borderless.

(PS: UDP, although a plurality of packets may also be transmitted once or more times a packet transmission, each message is bounded, so there is no problem of half a pack and stick package.)

Solution

As seen above, UDP reason why no stick pack and half a pack of problems, mainly because the message is not borderless, therefore, we can also take a similar line of thought.

Into a short connection

Short connection into a TCP connection, a request for a short connection. In this case, to establish a connection between the connection release message is the information transmitted, it generates a message boundary.

This method is very simple, you do not need to do too much modification in our application. But the disadvantage is also very obvious, inefficient, TCP connection and disconnection will involve three-way handshake and a four-way handshake, each message will be involved in these processes, wasteful performance.

Therefore, not Recommend this way.

Framing package

Encapsulated in a frame (Framing), which is a unit originally sent the message buffer size is now replaced by a frame, so that we could define the boundaries of the self. There are four general ways:

Fixed length

In this manner, the message is a fixed length to the boundary.

The advantage is to achieve is very simple, the disadvantage is that there is a huge waste of space, most of the message if passed relatively short, so there will be a lot of space is wasted.

Therefore, this approach is not generally referrals.

Separator

In this manner, the message boundaries is the delimiter character.

The advantage is no longer a waste of space, is relatively simple to achieve. The disadvantage is that when the content itself need escaping delimiter occurs, so it is transmitting or receiving, need to scan the entire contents.

Therefore, in this way efficiency is not very high, but you can try to use.

Special length field

In this manner, it is somewhat similar to Content-Length Http request, there is a dedicated field for storing the length of the message. As a server, when receiving the message, to parse the field (length field) fixed length acquiring the total length of the message, and then read the subsequent content.

Advantage is that precise positioning of user data, the contents do not escape. The disadvantage is that the length of the theory is limited, advance may need to limit the maximum length to define the length of the number of occupied bytes.

Therefore, very Recommend this way.

other methods

Otherwise it is not the same, and such use JSON can be seen as {}if in pairs. These advantages and disadvantages need to measure everyone in the respective scene.

The realization Netty

Netty support above encapsulated spoken framing (Framing) in the previous three methods, a brief introduction:

the way decoding coding
Fixed length FixedLengthFrameDecoder simple
Delimiter DelimiterBasedFrameDecoder simple
Special length field LengthFieldBasedFrameDecoder LengthFieldPrepender

to sum up

Today introduces the stick pack and half a pack of problems, solutions and support the idea of ​​Netty, I will next article focuses on the specific implementation of Netty, so stay tuned.

Are interested can visit my blog or follow me number of public, headline number, maybe there will be surprises.

https://death00.github.io/

Guess you like

Origin www.cnblogs.com/death00/p/11725918.html