1. Slide the window
As we all know, TCP is a reliable protocol, and reliability in many cases comes from TCP’s acknowledgement retransmission mechanism. On the basis of acknowledgement retransmission, sliding window protocol is implemented. Sliding window has two main functions:
- Ensure TCP reliability;
- Provides TCP flow control features.
1.1 Sliding window ensures reliability
The so-called window is actually a sequence of bytes stored in the sending buffer that can be sent by the sender. It is a continuous range, and we call it a window. With a slide, the window moves forward in the buffer as the data is sent. Such as:
- After the TCP connection is established, USER B tells user A the size of the receiving window, for example, 20.
- Then A sends 11 bytes, the sending window position remains the same, and B receives the out-of-order data;
- Only after A successfully sends the data and receives the acknowledgement returned by B will the sending window be moved forward in sequence, so as to avoid repeated sending of data after timeout. Note: If B does not return the acknowledgement within the specified time, then A will send it repeatedly
1.2 Flow control through sliding Windows
There are two main points to grasp in flow control. First, TCP uses sliding window to realize flow control mechanism; The second is how to consider the transmission efficiency in flow control.
1.2.1 Flow control
Flow control is an end-to-end control in which the receiver sends information to the sender so that the sender does not send data too fast. The main way to do this is to include the size of the receiving window in the returned ACK and use the size to control the data sent by the sender.
This involves A situation where B has already told A that its buffer is full, so A stops sending data; After waiting for A period of time, B of A buffer appeared to spare, so send A message tell my RWND A size of 400, but the message unfortunately lost, so there is A waiting for B notice | | B waiting A deadlock state to send data. Are introduced in order to deal with this problem, the TCP continue timer (personal timers), when A zero window receives notification, will enable the timer, the time is to send A probe 1 byte packets, at the same time, the other person will respond to their receiving window size, if the result is not 0, then reset for the timer, continue to wait.
1.2.2 Transmission efficiency
An obvious question is: a single sending a single byte, and the window there is a spare immediately notify the sender sends a byte, no doubt increased the many unnecessary packet in the network, so our principle is to send as much as possible a few bytes, or window to spare more time to inform the sender to send multiple bytes at a time. For the former, we widely use Nagle algorithm, namely:
- If the sending application process wants to send the sent data byte by byte to the TCP send cache, the sender sends the first data byte first and caches the following bytes first.
- When the sender receives the first byte of acknowledgement (and also obtains the network condition and the receiving window size of the other party), it forms the remaining bytes in the buffer into a packet of appropriate size and sends it out.
- When the data reaches half of the size of the sending window or the maximum length of the packet segment, a packet segment is sent immediately. For the latter, we usually ask the receiver to wait for a certain amount of time, or to get enough space for a segment, or to wait until the receive cache is half empty, before telling the sender to send the data.
2. Congestion control
Computer in the network bandwidth, switching nodes in the cache, routers, and so on are the resources of the network, they can provide the available resources are limited, if a certain time, the demand for a resource in the network more than its parts are available, and the performance of the network will become bad, like a traffic jam, car too much more than the number of sections of the load, is congested, The analogy to a network is congestion control. Note: Congestion control is a global process, and flow control in sliding Windows is point-to-point traffic control. They are fundamentally different.
TCP congestion control consists of four core algorithms: slow start, congestion avoidance, fast retransmission, and fast recovery.
2.1 Slow start and congestion avoidance
The sender maintains a state change called the congestion window. The size of the congestion window depends on the congestion degree of the network and changes dynamically. The sending window of the sender may be equal to the congestion window, or the sending window may be smaller than the congestion window because the receive cache of the receiver is insufficient.
The idea of the slow start algorithm is that, instead of sending a large amount of data at the beginning, the network congestion needs to be detected first, and the size of the congestion window gradually increases from small to large.
Congestion avoidance, then there is a slow start threshold SSthresh state variable in congestion control. Assuming the size of the congestion window is Cwind, then there are the following situations:
- When cwind < ssthresh, that is, the slow start algorithm is executed. When the current congestion window is used to send data, after receiving multiple acknowledgments, the Cwind is doubled and the transmission continues.
- When cwind > SSthRESH, perform congestion avoidance algorithm, cwind+1, and then continue to send data;
- When the network sends congestion, update ssthRESH to half of the ssthRESH value before congestion, set cwind to 1, and continue to perform according to the above two conditions.
2.2 Fast retransmission and Recovery
In fact, the fast retransmission algorithm requires the sender to immediately retransmit the unreceived packet segments as long as it receives three consecutive double acknowledgements, instead of waiting for the set retransmission timer. Fast retransmission is used in conjunction with fast recovery, in the following two cases:
- When the sender receives three consecutive repeated acknowledgements, the multiplication reduction algorithm is executed to halve SSthresh, but the slow start algorithm is not executed;
- In another case, considering that if the network is congested, the sender may not receive three consecutive repeated acknowledgments, it will think that the network is not congested. Therefore, the slow start algorithm is not executed at this time, but the Cwind size is set to SSthresh and the congestion avoidance algorithm is executed.
Globally, TCP congestion control effectively ensures fairness among data streams. Once a packet is lost, it immediately halves and exits, leaving enough space for other new data streams to ensure fairness.
3. Reconnect the cable
As the name implies, the network should be reconnected after disconnection. In network programming, disconnection mechanism is a must. So how to design a disconnection mechanism?
3.1 The program sets a fixed reconnection time
There are two cases:
- First, after finding the broken line, reconnect once immediately, and then reconnect after an interval of 2 seconds, and then 4 seconds, 6 seconds, 8 seconds, etc.
- Two is 2 seconds, 4 seconds, 6 seconds, 8 seconds to reconnect like this;
3.2 Let the customer set
After disconnection, a pop-up window is displayed on the interface for the customer to set the reconnection interval. This is reflected in many desktop clients and mobile apps.
3.3 Monitoring network Status
We can get the network, and if it’s disconnected, we don’t try to reconnect it, but if it’s disconnected, we try to reconnect it.