TCP data stream classification
Various TCP-based solutions can be roughly divided into two categories based on data throughput:
(1) interactive data types, such as telnet and SSH. In most cases, this type of protocol only performs small-traffic data exchange, such as pressing the keyboard and displaying some text.
(2) Data is block type, such as ftp. This type of Protocol requires that TCP can carry data as much as possible, maximize the data throughput, and improve the efficiency as much as possible.
For these two cases, TCP provides two different policies for data transmission.
TCP interactive data stream
Generally, when the network speed is very fast, for example, using the lo interface for telnet communication, when the primary key is pressed and the ECHO is required, the client and server will go through the process of sending key data-> ack for sending key data on the server-> ack for sending echo data on the client, among them, the data traffic will be 40bit + 41bit + 41bit + 40bit = 162bit. If it is in the WAN, the TCP traffic of such small groups will cause a great network burden.
For highly interactive applications, TCP provides two policies to improve transmission efficiency and reduce network burden (but does this increase the latency of data interaction, is it understood that reducing the network burden increases Data Interaction ):
(1). Seek with Ack.
(2). Nagle algorithm (send as much data as possible at a time ).
(1) Sending method with ack on the producer
This policy means that after the host receives the TCP datagram from the remote host, it usually does not immediately send the ACK datagram, but waits for a short time, if there is a TCP datagram sent from the host to the remote host during this time, the ACK datagram "rst" will be sent out, and the original two TCP datagram will be integrated into one. Generally, this time is 200 ms. The timer here is based on the kernel and does not correspond to when data is received.
(2). Nagle algorithm (send large data packets as much as possible)
People who have been on BBS should have feelings, that is, posting when the network is slow. Sometimes, after typing a string, after a period of time, the client suddenly shows a lot of content as if it were "crazy, it's like the data passes through all the time. This is the role of the Nagle algorithm.
The Nagle algorithm indicates that when host a sends a TCP datagram to host B and enters the status of ack datagram waiting for host B, only one TCP number is reported in the TCP output buffer, in addition, this datagram constantly collects subsequent data and integrates it into a large data packet. When the ACK packet of host B arrives, the data will be sent "in a brain. Although such a description is inaccurate, it is still an image and easy to understand. We can also appreciate the benefits of this policy to reduce the network burden.
When writing a plug-in program, you can disable this algorithm through tcp_nodelay. In addition, this algorithm is used to check the situation. For example, if the X Window Protocol Based on TCP still uses this algorithm when processing mouse events, the "latency" may be very high.
Block data stream of TCP
For FTP, which has high requirements on data throughput, we always want to send as much data as possible to the other host, even if it is a little "delay. TCP also provides a set of policies to support such requirements. In TCP, 16 bits indicate the size of the window, which is the core of these policies.
(1). Ack problems during Data Transmission
Before interpreting the sliding window, you need to check the ACK response policy. Generally, if the sender sends a TCP datagram, the receiver should send an ACK datagram. But in fact, this is not the case. The sender will continuously send data to fill the receiver's buffer zone as much as possible, and the receiver only needs to send an ACK message to respond to the data, this is the cumulative feature of ack, which greatly reduces the burden on the sender and receiver.
(2) Sliding Window
A sliding window essentially describes the data of the receiver's TCP datagram buffer size. Based on this data, the sender calculates the maximum amount of data that can be sent. If the sender receives a TCP datagram whose window size is 0, the sender stops sending data and waits until the sender sends a datagram whose window size is not 0.
For the sliding window protocol, the book also introduces three terms:
Window combination: when the window is close from the left to the right, this happens when the data is sent and confirmed.
Window Opening: when the right side of the window moves to the right, this phenomenon occurs after the receiver processes the data.
Window contraction: this phenomenon does not often occur when the right side of the window moves to the left.
(3). Data congestion
The above policy can be used for LAN transmission, but problems may occur when used in the wide area network, the biggest problem is that a large amount of data congestion (congestion) occurs when a bottleneck occurs during transmission (for example, it must go through a slip low-speed link). To solve this problem, the TCP sender needs to confirm the maximum data throughput of the lines connecting both parties. This is the so-called congestion window. (The network is not congested, and the data throughput is also up)
The principle of the congestion window is very simple:
1. the TCP sender first sends a datagram and waits for the response from the other party;
2. After receiving a response, double the size of the window and send two data packets consecutively;
3. After the other Party responds, double the window and send more data packets;
4. Wait until a timeout error occurs.
In this way, the sender understands the line carrying capacity of both parties, determines the size of the congestion window, and the sender sends data using the size of the congestion window. It is very easy to observe this phenomenon. Generally, when downloading data, the speed is slowly "Rushed up ".