The problem of sticking and half-package in socket programming and its processing

Source: Internet
Author: User

In general when the socket processing large data transmission will produce sticky and half-packet problems, and sometimes TCP in order to improve efficiency will buffer n packets and then sent together, this is related to the cache and the network.

Sticky pack for x.5 bags

Half Pack for 0.5 packs

Due to network reasons once may come 0.5/1/2/2.5/.... A bag

When you receive it, take a look at how many full packages there are in the package. The whole package is processed, which means the X is processed. The remaining 0.5 remain in the receiving area, waiting for the next reception.

This time Received is 0.5+1.5/0.5+1.3/0.5+0.5 ... The complete package has been processed, there are incomplete throw away 0.8.

The general situation receives the correct one to give the sending end a reply. Does not give an answer to the calculation timeout, the sender will be re-sent.

You can't throw a head without a tail.

No head, no tails to throw.

Symbolizing but the missing thing can be thrown

Symbolizing not lack of things can not be thrown

The reason for the sticky and half-packet phenomenon is that there is only the concept of flow in TCP, without the concept of package.

You can use the UDP protocol. This makes it possible to differentiate each package. However, to ensure that the package is lost. To mention efficiency, consider writing a sliding window to receive the bundle.

If the TCP protocol is used for transmission, it is necessary to separate each packet. There are three ways to do this. Because TCP is flow-oriented. Flow is only open and closed, you have to use a stream to lose multiple packets, it is necessary to distinguish each package.

One: can be sent each time the same size of the package, too large package is not sent, too small package, the latter part with a fixed character ' "" to fill.

Two:: The stream by character processing, extract a character to do the escape character (usually Java with ' \ ' To do the escape character, such as "\ n" for the newline). If you set the ' \ ' as the escape character, the sender will be appended with a ' \ ' in the back, if the package ends, use ' \ ' Make a bag of terminator. In this way, in the receiver, if read a separate ' \ ' or the end of the stream, the preceding content constitutes a package, if read two consecutive ' \ ', will be two ' \ ' with a ' \ ' to replace. This will ensure that the information in the original package is not changed, and that each package can be distinguished.

Three: When the sender sends a packet, the length of the package is sent to the other party (typically 4 bytes for the packet length), and then the contents of the package are sent past. The receiver receives 4 bytes, looks at the length of the packet, and then receives the packet by length, so that it does not go wrong. The above three methods are commonly used in network transmission. The latter two are very common. The last one is the most widely used in TCP long-connection transmissions. In general, it is to encapsulate a layer of protocol above the TCP protocol, which is used for the information exchange of subcontracting.

General processing is: A buffer that holds the read cache for the current connection

When there is data, buffer = buffer + DataIn, no-stop reception

After completion, start parsing buffer,

According to the protocol of the package, constant parsing buffer, and form a packet of processing, after processing, Buffer = Buffer-data, and continue to unpack

Complete.

The problem of sticking and half-package in socket programming and its processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.