No message boundary problem for "Go" TCP protocol

Source: Internet
Author: User

When writing an application using the TCP protocol, you need to consider a problem: The TCP protocol is no message boundary, that is, data from a single send method cannot be guaranteed to be read by a single receive method.

eg

First send: ABCDEFG second send: 123456 when the receiving party receives the data, the following conditions may occur:

First time receive: Abcdefg123456 may also appear: First receive: ABC second receive: Efg12 third time: 3456

Therefore, to resolve the TCP Send message and receive message consistency, programming must resolve the message boundary problem.

Workaround: 1, send a fixed-length message. This kind of square is suitable for the information length fixed occasion.

2. Send the message length with the message. This method increases the amount of data transmitted and increases the amount of programming effort.

Eg: the length of this information is indicated by 2 bytes in front of each sent string message, and the receiver obtains the length of the information from the two bytes first. The string that is sent by the sender is then looped according to the length value

3. Separate messages with special tags. This method is suitable for situations where the information itself does not contain special tags. If you send a line of information, you can use carriage return as a delimiter.

No message boundary problem for "Go" TCP protocol

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.