TCP Network Transmission "Sticky Pack" problem, classic solution (with code)

Source: Internet
Author: User

First, preface

On the TCP network transport sticky Packet, many people on the web wrote the principle. Sum up in a nutshell: Here is the server and client long connection, between the server and the client through the signaling transmission to do the description:

When the server is sent in accordance with a line of signaling sent to the operating system network layer, first into the buffer pool, and then the TCP protocol layer from the pool to obtain data, transfer to the client. We know that there are several scenarios for TCP transmission, such as sliding windows, 1-bit scenarios. Therefore, the data received by the client is impossible to complete a signaling.

A personal understanding of the concept of TCP sticky packets: It describes a scenario: "Signaling is next to each other, as if it were glued together." We have to do a necessary task before we take the signaling apart: structured data. The socket data is structured so that the original binary signaling is out.

Second, analysis

As I said just now, the data received by the client from the socket is indeterminate, possibly 1Byte, and possibly 100Byte. And even with the same signaling, the content is different in length. Picking up the signaling requires a key attribute: signaling length. Usually the two bytes of the signaling is the length, which indicates the length of the signaling. The client needs a buffer for regular signaling.

Typically the client receives the socket data, and there are several scenarios:


(Fig. 1)

2 shows: The first two bytes of signaling (orange callout) is 200B, but the client's buffer is only received 100B, then clients do nothing, waiting for subsequent socket data


(Fig. 2)

2 shows: The first two bytes of signaling (orange callout) is 50B, but the client buffer has exceeded 50B, then the client can intercept 50B, as a complete signaling, to the subsequent logic processing.


(Fig. 3)

3 followed, similar to Figure 2.

Third, the realization of logic

It is important to note that when you get the first two bytes, you need to determine whether the system is big-endian or small

/** * @brief Receive Message * * @param data pointer * @param length data length */void onreceivedata (nsdata * data) {if (data = NULL) {    Return    } [M_data Appenddata:data];    Const Byte *packagedata = (Byte *) [m_data bytes];    Nsuinteger packagesize = [m_data length];    Parse packet unsigned short messagesize = 0;    Nsuinteger pos = 0; while (Pos < packagesize) {if (pos + 2 < Packagesize) {//can read message packet-size//Read Mes Sage packet-size messagesize = * ((unsigned short *) (Packagedata + pos));//Current small head//byte tmp = (messag            Esize & 0xff00) >> 8;            Messagesize <<= 8;            Messagesize |= tmp;                    if (messagesize <= packagesize-pos) {//There is a complate message if (m_callback) {                M_callback->ondelivermsg ((Packagedata + pos), messagesize);                } pos + = Messagesize;            Continue  }} break;  }//deal with the last bytes. if (Pos < packagesize) {[M_data replacebytesinrange:nsmakerange (0, Packagesize-pos) withbytes: (Packagedata + P        OS)];    [M_data Setlength:packagesize-pos];    }else{[M_data setlength:0]; }}

Finish

TCP Network Transmission "Sticky Pack" problem, classic solution (with code)

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.