preface

  • The basics of computer networks are knowledge that programmers need to master, but are often ignored
  • Today, I’m going to detail the most important aspects of computer networkingTCPProtocol, including its features, three-way handshake, four-way wave, error-free transmission and other knowledge, I hope you will like it.

Before reading this article, please understand the basics of computer networks: present a comprehensive and detailed study guide for the basics of computer networks


directory


Definition 1.

Transmission Control Protocol

  1. Transport layer protocol
  2. Based on theTCPThe application layer protocols areHTTP,SMTP,FTP,TelnetPOP3

2 the characteristics of

  • Connection-oriented, byte stream oriented, full duplex communication, reliable
  • Detailed introduction is as follows:


3. The advantages and disadvantages

  • Advantages: Reliable data transmission
  • Disadvantages: Low efficiency (need to establish connections, send confirmation packets, etc.)

4. Application Scenario (Application layer protocol)

When communication data is required to be reliable, that is, data must be accurately transmitted to the other party

For example, file transfer: HTTP, HTTPS, FTP, etc. Mail transmission: POP, SMTP and other protocols

  • The world wide web:HTTPagreement
  • File transfer:FTPagreement
  • Email:SMTPagreement
  • Remote terminal access:TELNETagreement

5. Format of the packet segment

  • TCP is byte stream oriented, but the transmitted data unit = packet segment
  • Packet segment = header + data 2 parts
  • All functions of TCP are reflected in the functions of the fields in the header. Therefore, the following section describes the header of the TCP packet segment
  1. Fixed first 20 characters followed by 4N bytes are optional additions as needed
  2. Therefore, the minimum length of TCP header is 20 bytes


6. Establish the connection process

  • A TCP connection requires a three-way handshake
  • The details are as follows

After the TCP three-way handshake is successfully performed, a TCP connection is established to transmit application-layer data

note

  1. Due to theTCPFull-duplex communication is provided, so the application processes on both sides of the communication can send data at any time
  2. During any one of the three handshakes, no reply will be received

Note: Why does TCP need three handshakes to establish a connection?

  • Conclusion If the server receives invalid connection request packets, the server waits for requests from the client, resulting in deadlock and waste of resources

  • A detailed description

SYN flood attack:

  • As shown above, TCP resource allocation time on the server = when the second handshake is completed; The client TCP resource allocation time = the third handshake
  • This makes the server vulnerableSYNIn a flood attack, multiple clients initiate connection requests at the same time, requiring TCP connection resource allocation for multiple requests

7. Release the connection

  • At the end of the communication, both sides can release the connection, with a total of four waves of the hand
  • Specific as follows

Special note: why does TCP release connection need four wave?

  • Conclusion In order to ensure that both communication parties can notify each other of the need to release & disconnect the connection

After the connection is released, neither party can receive or send messages to the other party

  • A detailed description

Extended question: Why does the client wait 2MSL before closing the connection?

  1. namelyTIME - WAITWhat states do;
  2. MSL= Maximum packet segment life (Maximum Segment Lifetime)
  • Cause 1: To ensure that the last connection release confirmation packet sent by the client can reach the server, so that the server can release the connection

  • Cause 2: The connection request packet that was already invalid was prevented from appearing in the connection

    The client sends the last connection release request acknowledgement packet, and then passes 2MSLTime, so that all message segments generated during the connection duration disappear from the network.

That is, invalid connection request packets will not appear in the next new connection


8. Error-free transmission

  • Compared toUDP.TCPThe transmission is reliable and error free
  • So, whyTCPWhy is the transmission reliable and error-free?
  • Now, I’ll explain it in detailTCPError-free transmission of protocols

8.1 meaning

  • Error-free: that is, the transmission channel does not go wrong
  • Sending and receiving efficiency matching: No matter how fast the sender sends data, the receiver always has time to process the received data

8.2 Basic: Sliding window protocol

  • First understand two basic concepts: send window and receive window

  • Working Principle For sender:
  1. The send window slides forward one frame each time a confirmation frame is received
  2. When there are no frames to be sent in the sending window (that is, all frames in the window are sent but not confirmed), the sender will stop sending until the confirmation frame sent by the receiver makes the window move and there are frames that can be sent in the window. Then the sender will continue to send, as shown below:

For the receiving end: after receiving the data frame, move the window forward one position and send back the confirmation frame. If the received data frame falls outside the receiving window, it will be discarded.

Important features of the sliding window protocol

  • The send window can only slide forward if the receive window slides forward and the receiver sends the confirmation frame (only if the sender receives the confirmation frame)
  • The stop-wait protocol, the back N-frame protocol and the select retransmission protocol differ only in the size of the send window and the receive window:
  1. Stop waiting protocol: send window size =1, receive window size =1; That is, a single frame sliding window equals a stop-wait protocol
  2. Back N frame protocol: send window size >1, receive window size =1.
  3. Select retransmission protocol: Send window size >1, receive window size >1.
  • When the size of the receive window is 1, the order of frames can be guaranteed.
  • In the sliding window protocol of the data link layer, the size of the window is fixed during transmission (note to be different from the sliding window protocol of TCP).

8.3 An error-free transmission solution

Core idea: use some reliable transmission protocol, so that

  1. When an error occurs, let the sender retransmit the error data: that is, error retransmission
  2. When the receiver is too late to receive the received data, the sender can be notified to reduce the efficiency of sending data: speed matching
  • To solve these two problems, the solutions are automatic retransmission protocol and flow control-congestion control protocol

Solution 1: Automatic Retransmission Request Protocol ARQ (for error retransmission)

  • define

    Auto Repeat reQuest, detailed introduction is as follows:

  • type

The following three types of protocols are mainly explained

Type 1: Stop equation ARQ (stop-and-wait)

  • Principle :(single frame sliding window) stop-wait protocol + timeout retransmission

That is, the size of the sending window is 1, and the size of the receiving window is 1

  • The principles of the stop-wait protocol are as follows:
  1. For each frame sent, the sender waits for the receiver’s reply signal before sending the next frame
  2. Each time the receiver receives a frame, it sends back an acknowledgement signal indicating that the next frame is available
  3. If the receiver does not respond, the sender must wait

Type 2: Back N frame protocol

Also known as continuous ARQ protocol

  • Principle multi-frame sliding window + cumulative confirmation + back N frames + timeout retransmission

That is, the size of the sending window is greater than 1, and the size of the receiving window is equal to 1

  • A. Sender: using the principle of multi-frame sliding window, it can continuously send multiple data frames without waiting for the confirmation of the other party b. Receiver: using the principle of cumulative confirmation & back N frames, only allowed to receive frames in sequence. The specific principles are as follows:

Examples of

In this example = the source sends a data frame to the destination. Specific examples are as follows:

Type 3: Selective Repeat (ARQ)

  • Principle multi-frame sliding window + cumulative confirmation + back N frames + timeout retransmission

That is, the size of the send window is greater than 1, and the size of the receive window is greater than 1

Similar to type 2 (back N frame protocol), the difference here is only the size of the receiving window, so it is not described here too much

  • Advantages: The utilization rate of the channel is improved due to the continuous transmission of data frames. B. Lack: The transmission efficiency is reduced when the data frames that have been correctly transmitted must be retransmitted (only because there is an error in the data frame before these data frames)

Therefore, if the channel transmission quality is very poor, resulting in a large bit error rate, the back N-frame protocol is not necessarily superior to the stop-wait protocol

Solution 2: Flow control & Congestion Control (for speed matching)

Measure 1: Flow control

  • Introduction to the

  • The sample

  • Special note: deadlock issues


Measure 2: Congestion control

  • Definition prevents too much data from being injected into the network so that routers and links in the network do not become overloaded

Congestion: Demand for resources in the network > what the resource can provide

  • The difference from “flow control”

  • The specific solution is divided into two solutions: slow start & congestion avoidance, fast retransmission & Fast recovery

Four algorithms are involved, namely slow start & congestion avoidance, fast retransmission & fast recovery

The details are as follows

Solution 1: Slow start & Congestion avoidance

1.1 Reserve knowledge: congestion window, slow start algorithm, congestion avoidance algorithm

A. Congested Windows

  • The sender maintains one state variable: the congestion window(CWND, congestion window), detailed introduction is as follows

B. Slow start algorithm

  • Principle When the host starts to send data, the value of the congestion window (that is, the value of the sending window) increases gradually from small to large, so that the number of packets to be sent increases gradually from small to large

  • Objective To test network congestion when transmission starts

  • Specific measures

  • Schematic diagram

  • Pay special attention to

    Slow start Slow indicates that the window for sending packets is congested at the beginning(CWND)Set to small (1) so that the sender sends only one segment initially (to test the network for congestion)

This is not to say that congestion Windows (CWND) grow at a slower rate

C. Congestion avoidance algorithm

  • The principle of

    Congested Windows(CWND)It grows slowly in a linear fashion: Every round trip timeRTT, the sender’s congested window(CWND)Add 1
  1. Congestion Congestion is avoided and inevitable. However, the congestion window grows slowly according to the current law, making the network less prone to congestion
  2. Compared with the doubling of the slow start algorithm, the congestion window grows at a much slower rate
  • Schematic diagram

1.2 Solution Description (Slow Start & Congestion Avoidance)

  • To prevent window congestion(CWND)If network congestion is caused by excessive growth, slow start and congestion avoidance are adopted. The specific rules are as follows

  • Example is given to illustrate

Solution 2: Fast retransmission & Fast Recovery

The fast retransmission & fast recovery solution is an improvement on the slow start & congestion avoidance algorithm

2.1 Reserved knowledge: fast retransmission algorithm and fast recovery algorithm

A. Fast retransmission algorithm

  • The principle of

    1. The receiver sends repeated acknowledgements as soon as it receives an out-of-order segment (so that the sender knows early that a segment has not reached the other party), rather than waiting until it sends data
    2. Once the sender receives three consecutive acknowledgements, it immediately retransmits the unreceived packet segments without waiting for the expiration of the retransmission timer
  • Effect Because the sender retransmits the unacknowledged packet segment as soon as possible, fast retransmission can improve the overall network throughput by about 20%

  • Schematic diagram

B. fast recovery

When the sender receives 3 consecutive repeated acknowledgements, it:

  1. performMultiplicative decreaseAlgorithm: the slow start threshold(ssthresh)Set to half of the sender window value when congestion occurs = half of the congestion window
  2. The window will be congested(CWND)Value Set to the slow start thresholdssthreshHalved value = 1 and a half of the congested window
  3. Perform the additive augmentation algorithm: perform the congestion avoidance algorithm, so that the congestion window slowly increases linearly.

Note:

  1. The congestion window is skipped(CWND)A slow start process from 1, so it is called fast recovery
  2. The network is not congested. If the network is congested, the system does not receive multiple acknowledgement packets

2.2 Solution Description (Fast Retransmission & Fast Recovery)

  • Principle In order to optimize the solution of slow start and congestion avoidance, fast retransmission and fast recovery algorithms are added to the above solution. The specific rules are as follows

  • Schematic diagram

At this point, the knowledge of TCP error-free transmission is explained.


9. Differences with UDP


10. Summary

  • This article comprehensively explains the computer network in the most important TCP protocol, including its characteristics, three handshake, four wave, no error transmission and other knowledge, I believe you have a very good understanding of TCP protocol

  • I will continue to cover the basics of programming development. If you are interested, please follow the development notes for Carson_Ho


Please give the top/comment a thumbs up! Because your encouragement is the biggest power that I write!