Every interview is going to talk about TCP/IP at least a little bit, so what do you make of three handshakes and four waves?

What is “three handshakes, four waves”?

First we need to know that TCP is a connection-oriented unicast protocol.

Unicast protocol is generally a unicast routing protocol that forwards traffic to a certain location on the Internet from a source host to a destination host through a router. The Internet has at least two networks connected by routers. A router is a network-layer intermediary system that connects multiple networks together based on common network-layer protocols, such as TCP/IP. A network is part of a networking infrastructure (including Repeaters, hubs, and bridge/layer 2 switches) connected through routers and associated with the same network layer address called a network address or network ID.

TCP can be thought of as a byte stream that handles packet loss, duplication, and errors at the IP layer and below. During the establishment of a connection, the two parties need to exchange some parameters of the connection, which can be placed in the TCP header

So TCP provides a reliable, connection-oriented, byte stream, transport-layer service that uses a three-way handshake to establish a connection. Use 4 waves to close a connection.

Here is to

Three handshakes

Three-way handshake can you imagine such A scene, there are two both A and B, A to B some packages, B want to collect some packages, but afraid to B just sent the parcel, B went away, parcel arrived without charge, and B is afraid of A verbally said to send parcel, B waiting here for half A day realized that he had been tricked. So these two people need to come to a rule or an agreement to make sure that they both get what they want. This requires three handshakes.

Three handshakes: A says to B I’m going to send you some packages! B asked A, “Are you really going to send me A package?” Then A replied that I really really want to send you the package (B is just like the heroine of A romantic drama, A said that B is not believe the first time, must A repeat B letter) so far the agreement between A and B is reached, A can send the package to B.

It’s a metaphor, and it really shows:

(1) The first handshake:

The client sends a SYN packet (SYN = X) to the server, enters the SYN_SEND state, and waits for confirmation from the server.

(2) Second handshake:

After receiving a SYN packet, the server must acknowledge the syn (ACK = X +1) from the customer and send a SYN (ACK = Y) packet. In this case, the server enters the SYN_RECV state.

(3) The third handshake:

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

Note: there is no data in the packet sent by the above action. The client and server do not start transmitting data until the three-way handshake is complete. And ideally, once a TCP connection is established, it is maintained until either of the communication parties voluntarily closes the connection.

So why three handshakes?

Why three handshakes? Can’t I do two? I think if I say hair and you say yes, it’s over. If you have to be hypocritical, what’s the meaning of shaking hands for the third time?

First of all, why do we need to shake hands?

The client and the server need to connect before communication, and the “handshake” function is to prove that the client send ability and the server accept ability are normal, ** this is the “handshake” to achieve the purpose.

First handshake: the client sends a network packet and the server receives it. The server can verify that both the client’s sending capability and the server’s receiving capability are normal.

Second handshake: The server sends the packet and the client receives it. In this way, the client can conclude that the receiving and sending capabilities of the server and the client are normal. From the perspective of the client, I received the response packet sent by the server, indicating that the server received the network packet sent by me during the first handshake and successfully sent the response packet, which indicates that the receiving and sending capabilities of the server are normal. On the other hand, I received the response packet from the server, indicating that the network packet I sent for the first time successfully reached the server, so that my own sending and receiving capabilities are normal.

Third handshake: The client sends the packet and the server receives it. In this way, the server can conclude that the receiving and sending capabilities of the client and the sending and receiving capabilities of the server are normal. After the first and second handshakes, the server does not know whether the receiving capability of the client and its own sending capability are normal. On the third handshake, the server receives the client’s response to the second handshake. From the server’s point of view, my response data from the second handshake is sent out, and the client receives it. So, my ability to send is normal. The reception capacity of the client is also normal.

As you can see from the above procedure, a minimum of three handshakes are required. Both failed to reach the conclusion that their own and the other’s ability to receive and send were normal.

Four times to wave

TCP is a two-way peer-to-peer connection, in which both parties try to send or receive data to each other at the same time. When either party wants to close the connection, it sends a command to tell the other party that I’m closing the connection. An ACK is sent back, and the connection in one direction is closed. But the other direction can continue to transmit data, and when all data is sent, a FIN segment is sent to close the connection in that direction. The receiver sends an ACK to confirm closing the connection.

In layman’s terms

Four waves: A to B: I want to disconnect from you! B: Ok, break it! B also says to A I want to disconnect from you too!

A said: Ok, break it!

The real process is

(1) The first wave

The client sends a FIN to close the data transfer from the client to the server. That is, the client tells the server: I will not send you any more data (of course, the client will resend the data sent before the FIN packet if it does not receive the corresponding ACK packet), but the client can still accept the data at this time.

(2) The second wave

After receiving the FIN packet, the server sends an ACK to the client with the received SEQUENCE number +1 (the same as SYN, one FIN occupies one sequence number).

(3) The third wave

The server sends a FIN to close the data transfer from the server to the client, that is, to tell the client, my data has also been sent, will not send you data!!

(4) The fourth wave

After receiving the FIN, the client sends an ACK to the server and confirms that the number is + 1. The four-time wave is complete.

Why is it three handshakes to establish a connection, but four waves to close it?

In the LISTEN state, the server receives a SYN packet for establishing a connection and sends the ACK and SYN packets to the client. When a FIN packet from the peer party is received, the peer party only stops sending data but can still receive data. The upper-layer application determines whether to close the data transmission channel. Therefore, the PEER ACK and FIN packets are sent separately.