Socket sticky packet problem (GO)

Source: Internet
Author: User

Https://www.v2ex.com/t/234785#reply3

1.

This is the problem with IO for byte stream. The TCP protocol in the socket is a stream-oriented protocol, and the sender and receiver are not one by one corresponding. So the so-called sticky packet phenomenon. How to deal with it? Method 1: The protocol package is fixed long. The length of each packet sent out is fixed. For example, they are all 10 bytes. Collect 10 bytes Each time, when a complete packet. Method 2: Tell the package the length of the protocol header to start a fixed-length byte to inform the subsequent package length. Receiver pays first packet long bytes, know that the subsequent packet length and then collect. Method 3: Identify the end of the package with an end flag that never appears in the package body.

2.

My way of dealing with data based on the protocol header and protocol end to determine the message, so that is sticky packet, it is OK, when you receive the protocol end to determine the integrity of the entire message. Sticky packet is because the size of the sending buffer is larger than the data that needs to be sent, the data waits to reach the buffer size, the data is sent to the sticky packet, so in order to avoid sticky packets, after you send the data, empty buffer, you should be able to solve, but it is best to study TCP, this can avoid many problems.

Socket sticky packet problem (GO)

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.