Candidates : Interviewer Hello, can the interview begin?
Interviewer 161cd049b1fb6a: Well,
Interviewer : ’s talk about TCP today. Do you have any impressions of the various states of TCP?
Candidate : I still have some impressions, or I will briefly talk about the TCP three-way handshake and four-way wave process.
candidate : After talking about these two processes, we can cover the state of TCP
Interviewer : OK?
candidate : Before talking about TCP’s three-way handshake and four waved hands, I will draw you the TCP header format (:
candidate : For the TCP three-way handshake and four waved hands, our main thing is to pay attention to the sequence number, confirmation number and several flag bits (SYN/FIN/ACK/RST) of the TCP header.
candidate : Serial number: When a connection is established for the first time, both the client and the server will randomly initialize a serial number for "this connection". (Throughout the entire TCP process, the sequence number can be used to solve the problem of network packet disorder)
candidate : Confirmation number: This field indicates that the "receiving end" tells the "sending end" that the last data packet has been successfully received (the confirmation number can be used to solve the problem of network packet loss)
candidate : And the flag bit is easy to understand. When SYN is 1, it means that you want to create a connection. When ACK is 1, the confirmation number field is valid. When FIN is 1, it means that you want to disconnect. When RST is 1, it means that the TCP connection is abnormal and needs to be disconnected.
candidate : Let’s start with the three-way handshake, during which I will also talk about the TCP state involved in the three-way handshake.
candidate : The TCP three-way handshake process is actually: Confirm the serial numbers of the two parties (client and server) in communication
candidate : its process is like this
candidate : At the very beginning, both the client and the server are in the CLOSE state
candidate : The server actively monitors a certain port and is in the LISTEN state
candidate : The client will randomly generate a serial number (the serial number here is generally called client_isn), and set the flag bit to SYN (meaning to connect), and then send the message to the server
candidate : After the client sends the SYN message, it enters the SYN_SEND state
candidate : After the server receives the client's request, it also initializes the corresponding serial number (the serial number here is generally called server_isn)
candidate : fill in client_isn + 1 in the "confirmation number" field (equivalent to tell the client that the serial number sent has been received), and light up both the SYN and ACK flags (set to 1)
candidate : Send the message to the client, and the status of the server becomes SYN-REVD
candidate : After the client receives the message sent by the server, it knows that the server has received its serial number (you can know it through the confirmation number), and it has received the serial number of the server (server_isn)
candidate : At this time, the client needs to tell the server that it has received the serial number sent by him, so fill in server_isn+1 in the "confirmation number" field, and the flag bit ACK is 1.
candidate : the client enters the ESTABLISHED state after sending the message, and the server also enters the ESTABLISHED state after receiving the client’s message
candidate : This is the process of the three-way handshake and the TCP state involved
candidate : In summary, both parties send their serial numbers to each other to see if the other party can receive it. If "Acknowledgement is OK", then normal communication is possible. (In the process of the three-way handshake, you can see that both parties have the ability to receive and send)
Interviewer : the two handshake okay?
candidate : The two handshake can only ensure that the client's serial number is successfully received by the server, and the server cannot confirm whether its serial number is successfully received by the client. So it won't work (:
Interviewer : understands, then I want to ask why the serial number is random? And how is the serial number generated?
candidate : On the one hand, for security (random ISN can avoid attacks from non-same networks), on the other hand, it allows both parties in communication to discard segments that "do not belong" to the connection based on the sequence number.
candidate : How is the serial number generated? This...whatever you guess, it should be generated by calculations based on the "clock" and certain attributes of the TCP header, similar to the snowflake algorithm (: I forgot the details.
Interviewer : Since the network is unreliable, wouldn't the establishment of a connection go through a three-way handshake? What if it is lost midway?
candidate : Assuming that the first packet is lost, the SYN packet sent by the client to the server is lost (in short, the server did not receive the SYN packet from the client)
candidate receive the ACK packet from the server, it will periodically time out and retransmit until it receives the ACK from the server.
candidate : Assuming that the second packet is lost, the SYN+ACK packet sent by the server is lost (in short, the client did not receive the SYN+ACK packet from the server)
candidate : the server will not receive the ACK packet from the client for a long time, then it will periodically time out and retransmit until it receives the ACK from the client
candidate : Assuming that the third packet is lost (ACK packet), the client unilaterally enters the ESTABLISHED state after sending the third packet, and the server also thinks that the connection is normal at this time, but the third packet Did not reach the server
candidate : 1. If neither the client nor the server has sent data yet, the server will think that the SYN+ACK packet sent by itself has not been sent to the client, so it will retransmit its SYN+ACK over time Bag
candidate : 2. If the client is already sending data at this time, and the server receives the ACK + Data packet, it will naturally switch to the ESTABLISHED state and receive the client's Data packet
candidate : 3. If the server wants to send data at this time, but cannot send it, it will periodically time out and retransmit SYN + ACK until it receives the ACK packet from the client.
Interviewer : Well, are you going to wave your hands four times?
candidate : Well, after the connection is established, both the client and the server are in the ESTABLISHED state
candidate : Both parties have the right to disconnect. Let me take the client's initiative to disconnect as an example.
candidate : The client intends to close the connection and will send a FIN message to the server (in fact, the flag bit FIN is lit). After the client finishes sending, it enters the FIN_WAIT_1 state
candidate : After the server receives the FIN message, it will reply with an ACK message to the client (indicating that it has been received). After the server has finished sending it, it will enter the CLOSE_WAIT state
candidate : The client enters the FIN_WAIT_2 state after receiving the ACK message from the server
candidate : At this time, the server may still have data to send to the client. After the server confirms that it has no data to return to the client, it sends a FIN message to the client and enters the LAST_ACK state.
candidate : After receiving the FIN message from the server, the client responds to the ACK message and enters the TIME_WAIT state.
candidate : After the server receives the ACK message from the client, the server enters the CLOSE state
candidate : The client waited until 2MSL at TIME_WAIT and also entered the CLOSE state
candidate : This is the end of the four waved hands. Combined with the three-way handshake, each state of TCP has also been said.
Interviewer : Well, just waved four times after talking, then why do you think it was four times?
candidate : In fact, it is well understood that when the client sends a FIN message for the first time, it just means that the client no longer sends data to the server, but at this time the client still has the ability to receive data. When the server receives the FIN message, there may still be data to be transmitted to the client, so it can only reply ACK to the client first.
candidate : Wait until the server no longer has data to send to the client before sending a FIN message to the client, indicating that it can be closed.
Candidate : So, four times in one round.
Interviewer : from the four waved hands, there is a TIME_WAIT state. Do you know what this state is for? (Waiting for 2MSL)
candidate : There are two main reasons. 1. Ensure that the "receiver" of the last ACK message can be received (if not received, the other party will resend the FIN message) 2. Ensure that the remaining data in the previous network is lost when a new connection is created
candidate : In fact, it is easier to understand. Just like when we restart the server, we will gracefully shut down various resources first, and then leave it for a period of time. We hope that the resources will be shut down normally during this time, so that restarting the server (or publishing) will basically think that it will not affect the online It's running.
interviewer : What is the harm if the TIME_WAIT state is more than that? How to solve it?
candidate : From the perspective of the process, the TIME_WAIT state will only appear on the party that initiates the closing of the connection. The harm is that it will take up memory resources and ports (after all, waiting), if it is resolved, there are Linux parameters that can be set, and forget about it.
Interviewer : Let’s ask one more question today, we often talk about TCP connections, so what exactly is this connection? How do you understand?
candidate : In fact, what can be found from the three-way handshake is that TCP establishing a connection is nothing more than exchanging the status of both parties (such as sequence numbers). Then there is no more... The connection is essentially "just maintaining a state with each other, with connection characteristics."
Interviewer : Okay.
Follow my WeChat public [161cd049b2038d Java3y ] to talk about the difference!
[Online interviewer + writing Java projects from scratch] continuously being updated with high intensity! Please star! !
Originality is not easy! ! Seeking three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。