The ultimate answer on TCP stick package and unpacking

Programmers industry has some strange misconception (misconception), these misunderstandings very popular, and people hold erroneous views often emphatic killed not believe they are wrong, it is humorous. The reason is because these erroneous views corresponding to the correct point of view does not meet people's normal habits of mind, is a twisted person's intuitive feelings.

There are two misconceptions very classic, again and again come in threes, just like leeks, cut finish, but also longer and more. One is the classic "Up to 65 536 connect to the server" misunderstanding , open the link to see the presentation. The other is here to talk about the TCP "stick pack" and "unpacking" problem.

Based on the above ideas, we first introducer normal habits of mind, and then introduce the proper perspective distortion, so you will be impressive.

First of all, people's normal habits of mind that the data transmission should be based on the message, people either dialogue or write, is certainly a word or a sentence of a letter, which is called the message. The TCP is what is it? TCP is a streaming protocol, simply, when you use it, there is no so-called packets, both the chat saying or send pictures to TCP, it all are no boundaries, no TCP know these things, you do not know a word by line feed, you send the picture is a picture, everything is data flow does not have any boundaries. This is clearly not in line with people's normal thinking is distorted.

Whether it be people thinking, or the real thing, we must have borders. This is a contradiction.

So, for the programmer, before using TCP, you must be the message protocol design. Either you use someone else designed message protocol, or design your own. If you do not design, good design does not use someone else, then you are definitely wrong, you will encounter the so-called stick package and unpacking.

About message protocol design, this article has introduced  http://www.ideawu.net/blog/archives/429.html

General programmers do not want to design their own course, one can not afford, and second, do not need to re-invent. So after completing the design of it, it must be achieved. Because the level of the problem itself, or will encounter TCP stick package and unpacking problems. So, my advice is, do not own to achieve, you should go to someone else using code already written.

Then talk about if they directly interface using TCP Why do I get stick pack and unpack the problem? TCP interface simply only two:

send(data, length);
recv(buff);

People will understand based on the person's normal thinking to assume that send () is to send a message, recv () is to receive the message. However, as already mentioned, TCP is not the so-called packet boundaries, so you are wrong. Why is it wrong? Because the send () and recv () is not one to one. In other words, the number of calls send () the number of calls and recv () are independent, and sometimes are equal (when you are on your own machine and network test basically equal), sometimes are not equal (Internet on very prone).

Now understand it, TCP's send () and recv () is not one to one, of course, the package will stick to the application layer, the application layer packet demolition. After you understand how to solve? Simple, others with encapsulated code. Or, you slowly go on her own!

Correctly read the message from the TCP socket code must be long like this, if you do not grow like this, you are wrong!

char recv_buf[];
Buffer buffer;
// loop network: the network must be read in one cycle, because the network is a steady stream of data.
while(1){
    // reads from indefinite length for a TCP stream flow data, read data is no guarantee that your desired length
    tcp.read(recv_buf);
    // this will spliced ​​stream data and stream data received before together
    buffer.append(recv_buf);
    // parse cycle: the message must be resolved in a loop, to avoid the so-called stick package
    while(1){
        // attempt to resolve the message
        msg = parse(buffer);
        if(!msg){
            // the message was not ready, worse, we had the unpacking! Parsing out of circulation, continue reading network.
            break;
        }
        // the parsed data stream corresponding to the packet cleared
        buffer.remove(msg.length);
        // business processes
        process(msg);
    }
}

This code consists of two cycles: cycle and parsing loop networks.

Attention! If your code is not long like this, absolutely wrong! It must be wrong! Do not quibble! The correct approach is only one of thousands of wrong approach.

If you really want to solve the so-called TCP stick package and unpacking problem, you must write code like mine. If not, it is wrong, that you simply did not understand.

Related posts:

  1. Master-Workers high load mode processing
  2. Transmitting binary data via HTTP POST
  3. Gun fight TCP - sticking posters on the package unpacking question again and again
  4. Stop waiting mechanism to achieve data transmission
  5. Reliable transmission using Channel
Posted by  ideawu at 2017-06-02 15:02:56

Guess you like

Origin www.cnblogs.com/leijiangtao/p/12050381.html