- First handshake: when establishing a connection, the client sends a syn packet (syn = x) to the server and enters syn_ Sent status, waiting for server confirmation; SYN: synchronous sequence numbers.
- The second handshake: when the server receives the syn packet, it must confirm the syn (ACK = x + 1) of the client. At the same time, it also sends a syn packet (syn = y), that is, syn + ACK packet. At this time, the server enters syn_ Recv status;
- The third Handshake: the client receives the syn + ACK packet from the server and sends the ACK (ACK = y + 1) to the server. After the packet is sent, the client and the server enter the established (TCP connection successful) state and complete the three handshakes.
Why not shake hands twice
TCP is a two-way communication protocol, both sides have the ability to send information and receive response. If there are only two handshakes, at most only the initial serial number of the initiator can be confirmed, and the serial number selected by the other party cannot be confirmed
- The client process sends connection release message and stops sending data. Release the header of the data message, fin = 1, and its sequence number is SEQ = u (equal to the sequence number of the last byte of the previously transmitted data plus 1). At this time, the client enters the fin-wait-1 state. TCP stipulates that even if the fin segment does not carry data, it will consume a serial number.
- The server receives the connection release message, sends out the confirmation message, ACK = 1, ACK = u + 1, and carries its own serial number SEQ = v. at this time, the server enters the close-wait state. The TCP server notifies the high-level application process that the direction of the client to the server is released. At this time, it is in a semi closed state, that is, the client has no data to send, but if the server sends data, the client still has to accept it. This state will last for a period of time, that is, the duration of the whole close-wait state.
- After receiving the confirmation request from the server, the client will enter the status of fin-wait-2 and wait for the server to send the connection release message (before that, it needs to accept the final data sent by the server).
- After sending the final data, the server will send the connection release message to the client, fin = 1, ACK = u + 1. Because it is in the semi closed state, the server is likely to send some more data. Assuming that the sequence number is SEQ = w, the server will enter the last-ack state and wait for the client’s confirmation.
- After receiving the connection release message from the server, the client must send a confirmation, ACK = 1, ACK = w + 1, and its serial number is SEQ = u + 1. At this time, the client enters the time-wait state. Note that the TCP connection has not been released at this time. It must pass 2 * MSL (the longest message segment lifetime) before it enters the closed state when the client cancels the corresponding TCB.
- As long as the server receives the confirmation from the client, it will immediately enter the closed state. Similarly, after canceling the TCB, the TCP connection is ended. As you can see, the server ends the TCP connection earlier than the client
Why is there three handshakes when connecting and four handshakes when closing
Because when the server receives the syn connection request message from the client, it can directly send the syn + ACK message. The ACK message is used to reply and the syn message is used to synchronize. But when closing the connection, when the server receives the fin message, it is likely that it will not close socket immediately, so it can only reply to an ACK message and tell the client, “I received the fin message you sent.”. Only when all messages on my server end are sent, can I send fin messages, so they cannot be sent together. So it takes four steps to shake hands.