Introduction: This article is mainly to share about TCP, UDP, TCP three handshake, TCP four wave of personal understanding, try to use the vernacular way to explain, but certainly will be mixed with some professional terms, to this piece of ignorance of the small white brothers please forgive me.

What is THE TCP protocol?

The device (mobile phone or computer) can use the networking function because the underlying device implements THE TCP/IP protocol to enable the terminal to establish TCP connections over the network. TCP provides a channel for the upper-layer network to transmit data over an undifferentiated network.

It can be understood that TCP is equivalent to a connection channel, and HTTP and HTTPS are applications based on this connection channel: transporters.

A TCP three-way handshake establishes a connection, while a TCP four-way wave closes a connection.

Finally, on the OSI 7 layer network model (here do not understand the matter, combined with the following taste better)

Where does the TCP triple handshake and TCP quadruple wave occur?

To solve a problem without a problem drive is to go rogue,

To quote a common interview question: What happens between entering the URL and seeing the page? To understand the TCP three-way handshake and TCP four-way wave?

What happens between entering the URL and seeing the page? (This is only a simple step, because this article is intended to describe some TCP related issues in plain English.)

  1. URL Address Resolution
  2. Cache check
  3. The DNS
  4. TCP three-way handshake
  5. The data transfer
  6. TCP waved four times
  7. Page rendering

It can be seen that the TCP three-way handshake and TCP four-way wave mainly occur before and after the data transfer.

What is the TCP three-way handshake?

The client sends a request to the server to obtain data through IP. At this time, the client and the server need to establish a network connection channel (only after the network connection channel is established, we can obtain data from the server through IP address). The process of establishing the channel is the TCP three-way handshake.

TCP To make the channel more stable and reliable, the TCP three-way handshake is a three-way effect process.

Commonly used nouns for effect:

  • Seq Number, which identifies the byte stream sent from the TCP source to the TCP destination and is marked when the initiator sends data
  • Ack id. The ack id field is valid only when the ACK flag bit is 1. Ack =seq+1
  • Sign a
  • ACK: Confirms that the serial number is valid
  • RST: resets the connection
  • SYN: Initiates a new connection
  • FIN: Releases a connection

The process of effectiveness:

Three handshakes why not two, or four?

TCP as a reliable transmission control protocol, its core idea: to ensure reliable data transmission, but also to improve the efficiency of transmission!

  • In short, it’s not stable enough twice.
  • Four times is not very efficient

What is TCP four waves?

The purpose of TCP wave four is to disconnect the TCP connection channel.

The TCP four wave is initiated by the client as is the TCP three handshake.

So when does the client initiate the TCP four wave?

When a client sends information (headers, request bodies...) Once passed to the server, it initiates our TCP four wave request.Copy the code

Why is there a three-way handshake to establish a connection and a four-way wave to close it?

Theoretical explanation:

After receiving a SYN request packet from the client, the server can directly send a SYN+ACK packet. However, when closing the connection, the server may not close the connection immediately after receiving a FIN packet. Therefore, the server can reply only an ACK packet to tell the client: "I received the FIN packet you sent." I can send FIN packets only after all the packets on the server are sent. Therefore, I cannot send FIN packets at the same time.Copy the code

If closing the connection channel is also three times waved, it will look like this:

1. When the client sends information (request header, request body…) Once passed to the server, it immediately notifies the server that I am ready to close the connection channel

1. When the client sends information (request header, request body...) Once passed to the server, it immediately notifies the server that I am ready to close the connection channel 2. Because the data transmission content is relatively large and it takes time to prepare the data transmission content, the server and other response data content are ready and send the response data content and the reply client: I have received the notification that you want to disconnect to the client. (Waiting too long for a response, the client will wonder if the server has heard. This process will be relatively long and time-consuming. In this process, the client does not know whether the server is aware of the notification of disconnection, so it cannot prepare for disconnection. Client: Ok, bye.Copy the code

Such a process is clearly unreasonable.

Four waves made more sense:

1. When the client sends information (request header, request body...) Once passed to the server, it immediately notifies the server that I am ready to close the connection channel 2. Because the data transmission content is relatively large and it takes time to prepare the data transmission content, the server cannot transmit the data to the client at the first time, but it needs to tell the client: I know, you can disconnect after I transmit the data content to you. 3. The server sends the requested data back to the client. At the same time, the server tells the client: I gave you what you asked for 4. After receiving the data, the client tells the server: I received what you gave me, so I disconnect, goodbyeCopy the code

Combined with the above foreshadowing, it can be represented by the following picture: