This is the 14th day of my participation in Gwen Challenge

Basic TCP operations

  • A Host proactively initiates a connection with another Host, called SYN(Synchronization), to request Synchronization
  • One of the hosts disconnects, called FIN(Finish), and the request completes.
  • If one Host sends data to another Host, it’s called Push, data Push

In the above three cases, when the receiver receives the data, it needs to send the sender an ACK response. The premise of request/response reliability. If a request does not respond, the sender resends the request, assuming that the receiver did not receive it.

Establish a connection (three handshakes)

Three handshake steps

To ensure the reliability of the connection, each sent TCP data must have an ACK response.

  1. After the client initiates a connection to the server, the client sends a SYN packet (SYN= I) to the server and enters the SYN_SEND state for confirmation.

  2. When the server receives a SYN packet and acknowledges the SYN packet from the client, ACK= I +1. The server itself sends a SYN packet (SYN =j) to the client, along with an ACK(I +1) packet, and the server enters the SYN_RECV state.

  3. The client receives the SYN+ACK packet from the server and sends an ACK packet (ACK=j+1) to the server. After the ACK packet is sent, the server enters the ESTABLISHED state and completes the three-way handshake.

    parsing

1. Step 1 is the first handshake. During the first handshake, the client sends a packet to the server and waits for confirmation from the server.

2. Step 2 is the second handshake. After receiving the packet from the client, the server confirms that the ACK = SYN+1(I +1) from the client. Then the server sends a SYN+ACK packet with SYN=j and ACK= I +1 to the client. This represents the server Shouting to the client: “I received your message, your ability to send is ok, you sent me a SYN packet, I use ACK on the value of the SYN to reply to you, I also send you a SYN packet to confirm your ability to receive, you need to hear back to me an ACK also on the basis of the SYN I sent you +1, we establish a connection.”

3. Step 3 is the third handshake. The client receives SYN packets and ACK packets from the server, and then sends ACK= the value of SYN +1 to the server. “OK, I know your ability to send and receive is OK. Here is ACK, which is +1 based on the SYN you sent to me. It means that MY ability to receive is also OK.

Imaginary scene

A: “B, I like you”

B: “I know you like me. I like you, too.”

A: “Copy that. Let’s get together.”

To put it simply, the purpose of a TCP three-way handshake is to ensure that there is no problem with the sending and receiving capabilities of the client and server.

Disconnect process (four waves)

  1. When the client requests to disconnect, it sends a request to disconnect, known as the FIN.
  2. The server receives the request and sends an ACK to the client as a reply to the FIN.
  3. The server waits to close the connection and sends a FIN to the client.
  4. The client receives the FIN from the server, and then sends an ACK to the server after processing its own tasks (for example, it does not reply to the ACK for the message sent to the server).

If we follow the above understanding, can we merge steps 2 and 3 and send the ACK and its own FIN to the client at the same time? The answer is no, because the server may have its own resources to release, or the message sent to the client has not yet been answered, so the main difference from the three-way handshake is that the ACK+SYN packet is ACK and then SYN, and then only SYN in the disconnection phase is FIN.

Imaginary scene

A: “Break up, B”

B: “I know you want to break up, can I think about it?”

—- this will assume that B is reviewing his relationship with the server before processing the message

B: “All right, break it up, A”

—- At this time, A knows that B is going to break up and also starts to review the messages before the corresponding client processes them

A: “B, now that you know and agree, break it up.”

Why does TCP have 3 handshakes and 4 waves?

TCP is a duplex protocol. Both parties send a SYN(synchronous request) and an ACK(response) to the other party during connection establishment. When a SYN is sent to the other party, the other party can send an ACK-SYN message to the other party. So there are only three handshakes — three data transfers.

In the wave phase, both parties may have unfinished tasks, so they need to wait until one of them is finished before responding.