Wherever you go, you should remember that the past is false, memory is a never-ending road, all the past spring has ceased to exist, even the most tenacious and frenzied love is in the final analysis a fleeting reality. — Marquez

This article has been included in my GitHub, welcome everyone to participate in star and issues.

Github.com/midou-tech/…

Point attention, don’t get lost ❤️❤️❤️

Visit a forum to see a post, the title said he was learning network model, often mentioned TCP sticky packet problem, he laughed. This thread has a lot of discussion. Now that I see it, I want to explain the problem. TCP problem is also an important knowledge point in the computer network, of course, the interview is essential, and often encountered with the related problems in the work. Long Uncle not only about the network network knowledge points, other back-end knowledge points will often give you a nagging. Pay attention to me, wonderful content not too 💕 wechat search dragon Jump twelve can be wordless subscription.

A network model

The computer network layer model mainly has OSI seven layer model, TCP/IP five layer model, also has a kind of four layer model, four layer model will put the network card layer and the physical layer collectively called the network interface layer.

OSI seven-layer model exists in textbooks, TCP/IP five-layer model is the most widely used network architecture model in daily use. When learning network knowledge, we should grasp the key points to learn, and understand the seven-layer model.

As you can see from the above layers, TCP is a concept that exists at the transport layer. However, TCP has two meanings. One refers to THE TCP protocol, and the other is the general name of the TCP protocol family. Specifically, IP or ICMP, TCP or UDP, TELNET or FTP, and HTTP are all TCP/IP protocols.

What does the TCP layer do?

TCP is a protocol that belongs to the transport layer, and when I say TCP layer, I mean the transport layer.

The primary function of the transport layer is to enable communication between applications. This is what the TCP layer does in one sentence.

How does TCP layer data exist?

TCP is a connection-oriented, reliable streaming protocol. Concepts tend to be highly condensed classic goods, such as this sentence, covering TCP transport setup, transport mode, characteristics. Connection-oriented means that the transmission of two applications needs to establish a link in advance, which is our VIP channel to ensure that the traffic between the two applications is point-to-point transmission. The process of establishing a link is the three-way handshake.

The three handshakes and the four waves will be analyzed in the follow-up article. If you want to know, remember to follow me and search for Longyue twelve on wechat.

Streaming transmission means data transmission. TCP layer data interaction is streaming. What is streaming? Flow you can think of as a flow of water, which has no boundaries.

Reliable refers to the characteristic of TCP data transmission. Reliable means that the data you send is guaranteed to be received by the other application as much as possible. TCP implements sequence control or retransmission control to provide reliable transmission. In addition, it has many functions such as “flow control”, “congestion control” and improving network utilization. How to ensure message reliability through these mechanisms. The follow-up will be a corresponding article, do not remind me to pay attention to me.

It is clear from the definition that TCP data exists as a byte stream. The unit of data that TCP sends is called a data segment. The application interacts with TCP one data segment at a time (of varying sizes), and TCP treats the data handed over by the application as just a series of unstructured byte streams. TCP does not know the meaning of the byte stream being transmitted.

TCP does not guarantee that the recipient application data segment and the sender of the application data segment is given out by the corresponding relationship between the size of (for example, the sender application to sender TCP, a total of 10 data segment, but the receiver TCP may only four of the received data byte stream delivery upper applications). The byte stream received by the receiver application must be exactly the same as the byte stream sent by the sender application.

At what level is the package concept discussed?

  • A data Frame is a unit of information whose starting point and destination point are the data link layer.

  • Packet: Also a unit of information, which starts and ends at the network layer.

  • Datagram: A unit of information at the network layer that uses connectionless network services, usually at both its origin and destination.

  • Segment: Usually refers to a unit of information at the transport layer whose starting point and destination are both.

  • Message: A unit of information whose origin and destination are above the network layer (often at the application layer).

  • A cell is a fixed-length piece of information whose starting point and destination are the data link layer.

Elements are commonly used in switching environments such as asynchronous Transfer mode (ATM) and switched Multi-megabit Data Service (SMDS) networks.

A data unit is a number of units of information. Common data units include service data unit (SDU) and protocol data unit (PDU).

SDU transmits information between two layers on the same machine. A PDU is a message sent from each layer of the sending machine to the corresponding layer of the receiving machine (equivalent layer communication).

Packet: The basic unit of encapsulation that traverses the decomposition plane of the network layer and the data link layer. Usually a Packet maps to a Frame, but there is an exception: when the data link layer splits or combines several packets into a Frame.

The PDU at the data link layer is called Frame, the PDU at the network layer is called Packet, the TCP PDU is called Segment, and the UDP PDU is called Datagram.

A Datagram may be encapsulated into one or several Packets. Both frames and Packets are transmitted in the data link layer. Frame works at layer 2. Data link layer transmits data frames, including packets, and adds corresponding MAC addresses and layer 2 information. Data packets work at layer 3. The network layer transmits data packets, including data packets, and adds layer 3 information such as IP addresses used for transmission.

Why do interviewers and people still talk about TCP sticky packets?

It is easy to get out from above. First, TCP layer transport is streaming and does not send packets. Second, packet is the concept that exists at the network layer. TCP sticky packet problem?

Those of you who have studied networking from the top down know that an application first sends its own data over a socket. The application layer delivers structured data to TCP, and the structured data is streamed to the TCP layer.

The biggest problem with flow is that there is no boundary. Without boundary, data will stick together, which is called sticky packet. Of course, some students are going to ask, why is not called sticky section? Well…

Describe what a sticky bag is.

TCP sticky packets refer to packets sent by the sender and received by the receiver. From the view of the receiving buffer, the first packet is followed by the last packet.

Under what circumstances does sticky wrapping occur?

TCP is transmitted end-to-end and TCP connections are reusable. What does reuse mean? Multiplexing is when a connection can be used by multiple processes on a host.

** Sticky packet problems caused by TCP connection multiplexing.

If not reuse a connection only provide for the use of end-to-end two processes, this is the data transmission and the sender is agree on the format of the data, but multiple processes using a TCP connection, the different structure of data into TCP streaming, boundary division would be a problem of one sort or another.

If you set up a connection with the other party every time you send data through TCP, and then close the connection after the two parties send a piece of data, the sticky packet problem will not occur

Connection reuse, which will be covered in a series of TCP articles. So hurry to pay attention to me 😆, to prevent lost oh, wechat search dragon Yue twelve, can be worry-free subscription.

** Because TCP uses the Nagle algorithm by default, this algorithm can cause sticky packets.

Nagle algorithm mainly does two things: 1) only when the previous group is confirmed, the next group will be sent; 2) Collect multiple small packets and send them together when an acknowledgement arrives.

Multiple groups are assembled into one data segment and sent out. If there is no good boundary processing, sticky packets will occur during unpacking.

**3.** Sticky packets caused by too large packets.

For example, if the size of a message in the application process buffer exceeds the size of the send buffer, sticky packets may occur. Because the message has been split, it is possible that part of the message has been sent and received, but the other part may have just been put into the socket send buffer for further sending, which directly leads to the latter part of the received, directly causing the sticky packet problem.

**4.** Flow control and congestion control may also cause sticky packets.

** The receiver does not receive packets in the buffer in a timely manner, causing multiple packets to be received.

Most people know Nagle algorithm, the receiver is not timely processing two cases caused by the sticky package problem, but Long Uncle must remind you, the other several cases are also very common, the interviewer is super love to ask, if you can answer the other three, the probability of the interview is much greater.

How to deal with sticky bag problem?

1. If the Nagle algorithm is faulty, disable the algorithm according to application scenarios.

2. There are two ways to deal with other cases:

  • Tail tag sequence. Packet boundaries are represented by special identifiers, such as \n\r, \t, or some hidden character.

  • Header tags are received step by step. Add the data length to the TCP packet header.

  • The application layer sends data at a fixed length.

This article involves a lot of computer network key knowledge is not clear, but this article intends to let you understand the TCP sticky packet problem, other problems will be updated later. Pay attention to me, wonderful content is good.