The TCP three-handshake and four-wave question is one of the most common questions asked in an interview. Many readers know about triple and quadruple, but if you ask them a little deeper, they often can’t give an accurate answer.
This article attempts to illustrate this point with animation, in the hope that it will make it easier for readers to understand the nature of TCP interactions.
TCP/IP stands for Transmission Control Protocol/Internet Protocol and refers to a series of co-groups.
It can be divided into four levels:
-
1. Data link layer, network layer, transmission layer and application layer.
-
2. At the network layer, there are IP, ICMP, ARP, RARP and BOOTP protocols.
-
3, in the transport layer: TCP protocol and UDP protocol.
-
4. At the application layer, there are FTP, HTTP, TELNET, SMTP, DNS and other protocols.
TCP and UDP use the IP protocol to send packets from one network to another. Think of IP as a kind of highway that allows other protocols to travel on and find exits to other computers. TCP and UDP are the “trucks” on the highway, carrying the cargo of protocols like HTTP, FILE transfer protocol FTP, etc.
TCP and UDP are transport layer protocols used by FTP, HTTP, and SMTP. Although both TCP and UDP are used to transport other protocols, they have one significant difference: TCP provides guaranteed data transfer, while UDP does not. This means that TCP has a special mechanism to ensure that data passes safely and error-free from one endpoint to another, whereas UDP does not provide any such guarantees.
TCP three-way handshake
The TCP three-way handshake is like two people who see each other 50 meters apart on the street, but cannot be 100% sure because of smog and other reasons, so they have to wave to make sure that the other person knows them.
Hug each other
We see this process is a total of four actions, wave — nod and smile — wave — nod and smile. Two movements are performed in a row, first nodding and smiling (reply), and then waving again (seeking confirmation). In fact, the two movements can be combined, waving and smiling at the same time (SYN + ACK). The four movements are then simplified into three: wave — nod and smile and wave — nod and smile. This is the essence of the three-way handshake, with the middle movement being a combination of two movements.
We see that there are two intermediate states, SYN_SENT and SYN_RCVD, which are called “half on” and are waving at each other without seeing the other person nod and smile. Syn_sent is the “half open” state of the active open party, and SYN_RCVD is the “half open” state of the passive open party. The client is the active open side, the server is the passive open side.
-
syn_sent: syn package has been sent
-
syn_rcvd: syn package has been received
TCP data transmission
TCP data transmission is a conversation between two people at a distance, so you need to make sure that the other party heard you.
The client shouts a message (data), and the receiver replies that it heard it (ACK).
If you shout a message and do not hear a reply for a long time, you think your message has been blown away by the wind and do not hear it, so you need to shout again. This is TCP retransmission.
It is also possible that the Server hears the reply from the Client, but the reply from the Server to the Client is blown away by a strong wind, and the Client does not hear the reply from the Server. The Client can not determine whether its own words are blown away by the wind or the Server’s reply is blown away by the wind. The Client does not care, but retransmits it.
The Client can call the Server, and the Server can also call the Client. Because the TCP connection is duplex, both parties can initiate data transmission. But no matter which party propaganda, need to receive the other party’s confirmation to consider the other party received their propaganda.
The Client may be an anti-aircraft gun. After eight sentences, the Server does not reply one by one. Instead, after listening to the eight sentences, the Server replies to the other party saying that I heard all the eight sentences you said before. However, the Client cannot talk too much at one time. The Server may not be able to digest too much in a short period of time. Therefore, the two parties need to agree on an appropriate send and receive rate, which is called the TCP window size.
Summary of TCP three connections
(1**) First handshake: ** When A connection is established, client A sends A SYN packet (SYN= J) to server B and enters the SYN_SEND state, waiting for server B to confirm.
When server B receives A SYN packet, it must acknowledge client A’s SYN(ACK= J +1) and send A SYN packet (ACK= K). Server B enters the SYN_RECV state.
** Client A receives A SYN+ACK packet from server B and sends an ACK packet (ACK= K +1) to server B. After this packet is sent, client A and server B enter the ESTABLISHED state and complete the three-way handshake.
After completing the three-way handshake, the client and server begin transferring data.
TCP waved four times
The process of disconnecting a TCP connection is similar to the process of establishing a connection, except that the middle two parts do not always move in one step, so it is divided into four actions: Client wave (FIN) — Server smile (ACK) — Server wave (FIN) — Client smile (ACK).
The two actions in the middle are not merged because TCP has a “half-closed” state, which is a one-way shutdown. The Client has waved his hand, but the man is still there. He just stops talking, but his ears continue to listen. The Server continues to shout. When the Server was tired and stopped talking, the superclient waved his hand and the Client smiled sadly.
There is a very special state, time_wait, which is a long-term state that the closed party enters after replying to the other party’s wave. The standard duration of this state is 4 minutes. After 4 minutes, it will enter the closed state and release the socket resources. However, this time can be adjusted in the implementation.
It’s like the responsibility of the uninitiated breakup, you’re the one who asked for the breakup, and you have to pay the price. The consequence is a 4-minute time_wait in which socket resources (ports) cannot be released, similar to the widowhide period during which socket resources (ports) cannot be recycled.
It retransmits the last ACK packet to ensure that the peer party can receive it. If the peer does not receive an ACK, the FIN packet is retransmitted. The socket in time_wait state immediately resends an ACK packet to the peer.
At the same time, residual packets generated by the link on the Internet route during the conversation (the path is too rugged, the data packet takes too long, and the retransmitted packets are received, but the original packets are still on the road) are immediately discarded. Four minutes is enough time for the residual packets to disappear completely. Otherwise, residual packets may interfere with new links when new ports are reused.
4 minutes is 2 MSL, each MSL is 2 minutes. MSL is maximium segment lifetime. This time is defined by the official RFC protocol. I haven’t seen a very satisfactory explanation as to why 2 MSL instead of 1 MSL.
The four waves are not always four waves. Sometimes the two movements in the middle can be combined to create a three wave, and the active closing party will go directly from fin_WAIT_1 to time_wait, skipping fin_WAIT_2.
TCP four breakups summary
Because the TCP connection is full-duplex, each direction must be closed separately. The principle is that a party can send a FIN to terminate the connection in that direction when it has finished sending its data. Receiving a FIN only means that there is no data flow in that direction, and a TCP connection can still send data after receiving a FIN. The party that closes first performs an active shutdown and the other party performs a passive shutdown.
-
** Client A sends A FIN, which is used to shut down data transmission from client A to server B (packet segment 4).
-
Server B receives the FIN and sends back an ACK with the received sequence number plus 1(packet segment 5). As with the SYN, a FIN takes a sequence number.
-
Server B closes the connection with client A and sends A FIN to client A(packet segment 6).
-
Client A sends an ACK packet for confirmation and sets the sequence number of the received ACK packet to 1(packet segment 7).
conclusion
TCP state transition is a very complex process. This article only explains some simple basic knowledge points by analogy. More on TCP requires readers to search technical articles for further study. If the reader has a solid grasp of THE basics of TCP, the more advanced knowledge will not be too difficult to understand.
supplement
Recently as the interview manager in the interview, I found that many people are very strong framework, Java foundation is a little weak, many children are not very clear about HTTP, TCP, UDP and SOCKET concepts, silly not clear, here we also briefly mention
HTTP is itself a protocol for transferring hypertext from a Web server to a local browser.
HTTP(Hypertext Transfer Protocol) is a protocol that uses TCP to transfer information between two computers, typically a Web server and a client. The client sends an HTTP request to the Web server using a Web browser, and the Web server sends the requested information to the client.
The HTTP protocol is built on a request/response model. The client establishes a TCP connection with the server and sends a request to the server containing the request method, URL, protocol version, and associated MIME style message. The server responds with a status line containing the protocol version of the message, a success and failure code, and the associated MIME style of the message.