What are the differences between OSI layer 7 and TCP/IP Layer 4?

What is the difference between TCP and UDP?

  • Connection or not: TCP connection-oriented, UDP connectionless.
  • Reliability: TCP is reliable, using flow control and congestion control; UDP is unreliable and does not use traffic control and congestion control;
  • Number of connection objects: TCP can only be used for one-to-one communication. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interaction communication;
  • Transmission mode: TCP oriented byte stream; UDP Packet oriented;
  • Header overhead: The minimum TCP header is 20 bytes and the maximum TCP header is 60 bytes. UDP header overhead is small, only 8 bytes;
  • Application scenario: TCP applies to reliable transmission, such as file transfer. UDP is suitable for real-time applications such as IP telephony, video conferencing, live streaming, etc.

How does TCP achieve data reliability?

  • Flow control:

    • If the speed of the sender is too fast, the receiving buffer of the receiver will be full. At this time, data transmission will continue, resulting in a large number of packet loss, and then cause a series of problems such as packet loss and retransmission. TCP supports the transmission speed of the sender based on the processing capability of the receiver. This is the flow control mechanism. The specific implementation mode is as follows: The receiver puts the size of the receive buffer into the “Window size” field of the TCP header and notifying the sender through ACK.
  • Congestion control

    • TCP sends a large amount of data at the beginning of the transmission process, which may cause congestion if the network is very congested. So TCP introduces a slow start mechanism, which sends a small amount of data to explore the path before sending data.
  • Connection management

    • It’s a process of shaking hands three times and waving four times.
  • The checksum

    • In the process of data transmission, the data segment sent is treated as a 16-bit integer, and these integers are added up, and the carry in front cannot be discarded, and supplemented at the end, and then the inverse, to get the checksum. Sender: Calculates and populates the checksum before sending the data. Receiver: After receiving the data, the data is calculated in the same way to find the checksum and compare with the sender.
  • The serial number

    • TCP transmits each byte of data with a number, which is the serial number. The serial number is not only used for response, but also can be used to sort the received data according to the serial number and remove duplicate data.
  • Confirmation reply

    • During TCP transmission, each time the receiver receives data, it sends an ACK packet to the sender. The ACK packet contains the corresponding acknowledgement sequence number and tells the sender what data has been received and where to send the next data.
  • Timeout retransmission

    • During TCP transmission, the sender waits for the ACK packet sent by the receiver after sending some data, and parses the ACK packet to determine whether the data transmission is successful. If the sender does not receive an ACK packet from the receiver after sending data, the sender resends the sent data.

How does TCP improve transmission efficiency?

  • The sliding window

    • If we need to receive an ACK response before sending the next data segment for each sent data segment, we will be inefficient and spend most of our time waiting for an ACK response.
    • To improve efficiency, we can send more than one piece of data at a time, which can greatly reduce the wait time and improve performance. The window size refers to the maximum amount of data that can be sent without waiting for an acknowledgement.
  • Fast retransmission

    • Fast retransmission is also called high speed retransmission control.
    • If packet loss occurs, retransmission is required. Generally divided into two cases:
    • Case 1: The packet has arrived, but the ACK is lost. In this case, the loss of part of the ACK does not matter, because it can be confirmed by subsequent ACK;
    • Case 2: The packet is lost. The sender receives multiple identical ACK acknowledgments and immediately retransmits the lost data.
  • Delayed response

    • If the host receiving the data immediately returns an ACK response, the returned window size may be small.
    • Suppose the receiver buffer is 1 MB, and 512 KB of data is received at a time. If you answer immediately, the window returned is 512K;
    • In practice, the processing side may be so fast that 512K of data is consumed from the cache within 10ms.
    • In this case, the receiver processing is far from reaching its limit, even if the window is more enlarged, it can handle it;
    • If the receiver waits a little while to reply, for example, wait 200ms to reply, then this time the returned window size is 1M;
    • The larger the window, the higher the network throughput and the higher the transmission efficiency; Our goal is to maximize transmission efficiency without network congestion.
  • From the reply

    • On the basis of delayed reply, in many cases, the client server at the application layer is also a one – to – receive. In this case, piggy-back responses are often used to improve efficiency, and ACK responses are often transmitted together with data packets. Shake hands three times.

Do you know how TCP handles congestion?

Network congestion refers to the phenomenon that too many packets arrive on a certain part of the communication network. As a result, the network performance deteriorates or even the entire network performance deteriorates. In severe cases, network communication services stop, that is, deadlock occurs. Congestion control is a mechanism to deal with network congestion.

  • Four stages of congestion control:
    • Slow start
    • Congestion avoidance
    • The fast retransmission
    • Fast recovery

How about three handshakes and four waves?

First handshake: When the connection is established, the client sends a SYN packet (SYN =j) to the server and enters the SYN_SENT state, waiting for the server to confirm. SYN: synchronization Sequence Numbers.

Second handshake: After receiving a SYN packet, the server must acknowledge the client’s SYN (ACK =j+1) and send a SYN packet (ACK =k). Then the server enters the SYN_RECV state.

Third handshake: After receiving the SYN+ACK packet from the server, the client sends an ACK packet (ACK =k+1) to the server. After the ACK packet is sent, the client and server enter the ESTABLISHED state (TCP connection succeeds) and complete the three-way handshake.

1) The client process sends the connection release packet and stops sending data. The header of the data packet is released, FIN=1, and its serial number is SEq = U (equal to the serial number of the last byte of the data that has been transmitted plus 1). At this time, the client enters the FIN-wait-1 state. According to TCP, a FIN packet segment consumes a serial number even if it does not carry data.

2) After receiving the connection release packet, the server sends a acknowledgement packet with ACK=1, ACK= U +1 and its serial number seq= V. Then the server enters the close-wait state. The TCP server notifies the high-level application process, and the client is released to the server. At this time, the client is in the semi-closed state, that is, the client has no data to send. However, if the server sends data, the client still accepts it. This state will continue for a period of time, that is, the entire duration of the closewait state.

3) After the client receives the acknowledgement request from the server, the client enters the FIN-wait-2 state, waiting for the server to send the connection release packet (before receiving the last data sent by the server).

4) After the server sends the LAST data, it sends the connection release packet to the client, FIN=1, ACK = U +1. Since the server is in the semi-closed state, it is likely to send some more data, assuming that the serial number at this time is SEq = W. At this time, the server enters the last-ACK state, waiting for the confirmation from the client.

5) After receiving the connection release packet from the server, the client must send acknowledgement, ACK=1, ACK= w+1, and its serial number is SEq = U +1. At this TIME, the client enters the time-wait state. Notice That the TCP connection is not released, and the client enters the CLOSED state only after 2∗∗MSL (maximum packet segment lifetime) expires and the corresponding TCB is revoked.

6) As long as the server receives the confirmation from the client, it will enter the CLOSED state immediately. Again, canceling the TCB terminates the TCP connection. As you can see, the server ends the TCP connection a little earlier than the client.

Sequence number SEq: the sequence number is 4 bytes and is used to mark the order of data segments. TCP numbers all data bytes sent in the connection with a sequence number. The number of the first byte is randomly generated locally. After the bytes are numbered, each segment is assigned a number; Sequence number SEq is the data number of the first byte in the segment.

Ack: Indicates the number of the first data byte in the next packet segment that is expected to be received. The sequence number indicates the number of the first byte of the data carried in the segment. The acknowledgment number is the number of the next byte expected to be received; Therefore, the number of the last byte of the current packet segment +1 is the acknowledgement number. Acknowledgement ACK: The acknowledgement number field is valid only when ACK=1. When ACK=0, the acknowledgement number is invalid. Synchronization SYN: The serial number used for synchronization when the connection is established. When SYN=1 and ACK=0, this is a connection request packet segment. If the connection is agreed, then SYN=1 and ACK=1 are set in the response segment. Therefore, SYN=1 indicates that this is a connection request, or a connection accept packet. The SYN flag bit is set to 1 only when a TCP connection is being created. After the handshake is complete, the SYN flag bit is set to 0. Terminate FIN: Used to release a connection. PS: The uppercase words ACK, SYN, and FIN indicate the flag bit, which can be either 1 or 0. Ack, seq lowercase words indicate serial numbers.Copy the code

Parameter Meaning URG Indicates whether the emergency pointer is valid. Is 1, indicating that a bit needs to be processed first to determine whether the ACK confirmation number is valid. Generally, it is set to 1. The PSH prompts the receiving application to immediately read data from the TCP buffer. RST The peer party requests to establish a connection again and reset the connection. A SYN request establishes a connection and initializes the sequence number in its sequence number field. Set the connection to 1. FIN wants to disconnect.

Why does a TCP connection need three handshakes, but not two? Why?

The two handshakes are only the first step in a one-way connection, with the client sending a message to the server: Hello, server. In the second step, the server receives the message and replies a message to the client: Roger! Hello client. During this two-time handshake, the client sends greetings to the server, and the server receives the greetings, indicating that the client can normally send data to the server. However, the server sends greetings to the client, but the server does not receive feedback, so it cannot ensure whether the server can send messages to the client normally. After the third handshake, both sides can receive the data sent by the other side. Step 3: The client receives the message sent by the server. This proves that the client can receive the message from the server.

Why does the TIME_WAIT state return to the CLOSE state after 2MSL is passed?

A: Although we can enter the CLOSE state directly after all four packets have been sent, we must pretend that the network is unreliable and the last ACK may be lost. Therefore, the TIME_WAIT state is used to resend ACK packets that may be lost. The last ACK reply is sent on the Client, but the ACK may be lost. If the Server does not receive an ACK, it sends the FIN fragment repeatedly. So the Client cannot shut down immediately; it must confirm that the Server received the ACK. The Client enters the TIME_WAIT state after sending an ACK. The Client will set a timer to wait for 2MSL. If a FIN is received again within that time, the Client resends an ACK and waits for 2MSL again. The so-called 2MSL is twice the Maximum Segment Lifetime. MSL refers to the maximum lifetime of a fragment in the network, 2MSL is the maximum time required for a send and a reply. If the Client does not receive a FIN until 2MSL, the Client concludes that the ACK has been received successfully and terminates the TCP connection.

What if the connection is established, but the client suddenly fails?

TCP also has a keepalive timer, so obviously, if a client fails, the server can’t wait and waste resources. The server resets the timer every time it receives a request from the client. The time is usually set to 2 hours. If no data is received from the client within 2 hours, the server sends a probe segment and then sends it every 75 minutes. If there is no response after 10 consecutive probes, the server considers the client to be faulty and closes the connection.