Three-way handshake
In TCP, one end that initiates a request is called the "client", and the other end of the passive connection is called the "server 』. Both the client and the server can send and receive data after the TCP connection is established.
At first, both the server and client are in the closed status. Before communication starts, both parties must create their respective transmission control blocks (TCB ).
After creating a TCB, the server enters the listen status again. At this time, it is ready to receive connection requests from the client.
First handshake
The client sends a connection request message segment to the server. SYN = 1, ACK = 0, seq = X in the header of the message segment. The client enters the SYN-SENT state after the request is sent.
PS1: SYN = 1, ACK = 0 indicates that the packet segment is a connection request message.
PS2: X indicates the initial sequence number of the byte stream for this TCP communication.
TCP rules: packets with SYN = 1 cannot contain data, but consume a serial number.
Second handshake
After the server receives the connection request message segment, if it agrees to connect, it will send a response: SYN = 1, ACK = 1, seq = Y, ACK = x + 1.
The response enters the SYN-RCVD state after being sent.
PS1: SYN = 1, ACK = 1 indicates that the message segment is the response packet agreed by the connection.
PS2: seq = y indicates the initial sequence number of the byte stream sent when the server acts as the sender.
PS3: ACK = x + 1 indicates the number of bytes in which the server expects the next datagram to start from x + 1.
Third handshake
After the client receives the connection consent response, it also sends a confirmation message segment to the server, indicating that the connection consent response sent by the server has been successfully received.
The header of this article is ACK = 1, seq = x + 1, ACK = Y + 1.
After sending this packet segment, the client enters the established status. After receiving this response, the server enters the established status. At this time, the connection is established!
Why do I need three handshakes instead of two handshakes to establish a connection?
Prevents invalid connection request message segments from being received by the server, resulting in errors.
PS: Invalid Connection Request: if the connection request sent by the client to the server is lost, the client will send the connection request again after the response times out, the previous connection request is "invalid 』.
If you only need two handshakes to establish a connection, the client does not change much. You still need to get the server's response before entering the established State. The server enters the established State after receiving the connection request. In this case, if the network is congested and the connection request sent by the client fails to reach the server, the client times out and resends the request. If the server correctly receives and confirms the response, it is convenient for both parties to start communication. After the communication ends, the connection is released. At this time, if the invalid connection request arrives at the server, because there are only two handshakes, the server will enter the established status after receiving the request, waiting for data to be sent or actively sending data. However, the client has already entered the closed status, and the server will remain waiting, which wastes the server's connection resources.
Four Waves
The release of a TCP connection takes four steps. Therefore, it is called "four waves 』.
We know that the TCP connection is bidirectional, so in the four waves, the first two waves are used to disconnect one direction, and the last two waves are used to disconnect the other direction.
The first wave
If a considers that the data is sent completely, it needs to send a connection release request to B. This request only contains the packet header. The main parameters contained in the header are:
Fin = 1, seq = u. In this case, a enters the fin-wait-1 State.
PS1: fin = 1 indicates that the message segment is a connection release request.
PS2: seq = u, U-1 is the sequence number of the last byte A sends to B.
Second Wave
After receiving the connection release request, B notifies the corresponding application that the connection from A to B has been released. At this time, B enters the close-Wait Status and sends a connection release response to a. the packet header includes:
ACK = 1, seq = V, ACK = u + 1.
PS1: ACK = 1: In addition to the TCP connection request packet segment, the ACK of all data packets in TCP communication is 1, indicating a response.
PS2: seq = V, V-1 is the sequence number of the last byte B sends to.
PS3: ACK = u + 1 indicates that you want to receive the packet segment starting from u + 1 and have successfully received the first U bytes.
A receives the response and enters the fin-wait-2 status. Wait for B to send a connection release request.
After the second wave, the connection from A to B has been released, and B will not receive data, and a will not send data again. However, the connection between B and a still exists, and B can continue to send data to.
Third Wave
After B sends all data to a, B sends a connection release request to A. Request Header: fin = 1, ACK = 1, seq = W, ACK = u + 1. B enters the LAST-ACK state.
Fourth Wave
After receiving the release request, a sends a confirmation response to B. At this time, a enters the time-Wait Status. This status lasts for 2msl. If there is no Resend Request from B in this period, it enters the closed status to cancel TCB. After receiving the confirmation response, B enters the closed status and revokes TCB.
Why does a need to enter the time-Wait Status and wait for 2msl time before entering the closed status?
To ensure that B can receive a confirmation response.
If a directly enters the closed status after sending a confirmation response, if the response is lost, B will re-Send the connection release request after waiting for timeout, but at this time a has closed, no response is made, so B will never be able to close normally.
overview of computer networks transport layer three-way handshake and four-way handshake