1. Stop waiting for an agreement

If no response is received within the “timeout retransmission time”, the packet will be sent again. Therefore, note that:

  • After each packet is sent and a packet is sent, a copy of the sent packet should be saved temporarily.

  • Groups must be numbered;

  • Timeout retransmission should be longer than the average round trip for packet transmission.

The time-out retransmission protocol used here enables reliable communication over unreliable transport networks, also known as “automatic retransmission request” ARQ

2. Continuous ARQ

Send maintenance “send window”, in the send window can be sent consecutively, do not need to confirm next to each other, so as to improve channel utilization.

Recipients generally send acknowledgements in a cumulative manner rather than individually. Acknowledgement is generally sent to the last packet that arrives in sequence.

3. Format of the TCP segment header

Flag bit in TCP header:

  • RST: reset. When RST=1, it indicates that a serious error occurs in the connection and the connection must be released and a new connection must be established.
  • SYN: indicates synchronization. If SYN=1 and ACK=0, this is a connection request. If the receiver agrees to establish a connection, SYN=1, ACK=1.
  • ACK: confirm. The ACK number field is valid only when ACK=1. After a connection is established, ACK must be set to 1 for all packet segments.
  • FIN: Terminates. When FIN=1, the sender finishes sending data and requests to release the connection.

4. Flow control

Ensure that the sending rate of the sender is not too fast, and ensure that the receiver can accept it in time. The sending window of the sender cannot exceed the receiving window, and the UNIT of TCP window is byte, not packet segment.

When the receiving end reduces the receiving window to 0 during flow control, it means that the sender is not allowed to send data. When the receiving end has a free buffer, the receiving window is enlarged and sent to the sender to inform him that he can send data again. However, if the notification data is lost in the process of retransmission, the sender will wait for a long time. To break this endless loop, TCP sets a continuous counter for each connection. After each count, the sender sends a “probe packet segment” with a data segment of 1 byte for detection.

5. Congestion control

Congestion control is different from traffic control. Traffic control is controlled because the buffer of both parties is limited. Congestion control is related to Internet resources and bandwidth.

Congestion control is to prevent too much data from being injected into the network. TCP congestion control has four methods: slow start, congestion avoidance, fast retransmission, and fast recovery.

First of all, these four algorithms are window-based congestion control algorithms. The sender maintains a variable of “congestion window”, and the size of congestion window depends on the degree of network congestion. The sender makes its send window equal to the congestion window.

Our principle for controlling congestion Windows is: if congestion does not occur, increase the congestion window, if congestion occurs, reduce the window. So how do we tell if the network is congested?

If a timeout occurs, the network is congested.

Slow start: The initial congestion window starts from one MAXIMUM packet segment (SMSS) and grows twice as fast

Slow start threshold

When the blocking window < slow start threshold, use slow start algorithm;

When blocking window > slow start threshold, use congestion avoidance algorithm;

When blocking window = slow start threshold, both are available.

Congestion avoidance

Congestion avoidance means that the congestion window grows slowly by one SMSS at a time, rather than by twice as fast as the previous one at a slow start.

As shown in the figure above, the initial state congestion window starts from one packet segment, and then increases with the “slow start” algorithm. When the congestion window > the slow start threshold, the congestion avoidance algorithm is used. When the congestion window is 24, that is, point 2, and a timeout occurs, the congestion window is reset to one packet segment and the slow start threshold is set to half of the threshold before the timeout =24/2=12. Three redundant ACKS occur at “4”, which uses the “fast retransmission” algorithm to immediately notify the sender of the loss of a packet segment. Since some packet segments are lost, the “slow start” algorithm is not enabled, but the “fast Recovery” algorithm is enabled: Adjust the slow start threshold = congestion window /2 and set the congestion window to half of its current value, then start with the congestion Avoidance algorithm directly.

6. Establish and release the TCP connection

Three handshakes:

Four waves:

Note the time-wait state. After receiving the FIN information from the receiving end, the sender must WAIT 2MSL (maximum packet segment life) before entering the CLOSED state. There are many reasons:

(1) When the client sends the last ACK, if it is closed immediately, if the ACK is lost and not transmitted to the receiver, then the receiver still thinks that the connection exists and continues to maintain the connection. To prevent this situation, if the sender receives the resend message within 2MSL, it can resend the ACK message to close the connection normally.

(2) To prevent the “invalid connection request message segment” from appearing in this connection, the sender sends the last ACK 2MSL can make all the information of this connection disappear from the network, then the establishment of a new connection will not be affected by the “invalid connection request message segment” interference.

Life timer

When the client is disconnected due to a serious error, the server may not know that the client has been disconnected for a long time and continue to maintain the connection resources, resulting in resource waste. In order to eliminate this problem, have a keep alive timer, TCP server for each from the client receives the message segment will reset the keep alive the timer, if the keep alive timer period without information from the server is active, send message segment detection if 10 consecutive detection message segment is still no response, you can think that the client fails, close the connection (similar to a heartbeat package).