Candidate: Hello, interviewer, may I begin the interview
Interviewer: Well, go ahead
Interviewer: Today, let’s talk about TCP. Do you still remember the various states of TCP?
Candidate: I still have a few impressions, or I will briefly talk about TCP’s three-way handshake and four-way wave flow
Candidate: With these two flows, you can cover the state of TCP
Interviewer: I think so
Candidate: Before I talk about TCP’s three-way handshake and four-way wave, let me draw the TCP header for you.
Candidate: For the TCP three-way handshake and four-way wave, the main focus is on the TCP header sequence number, acknowledgement number, and several flag bits (SYN/FIN/ACK/RST).
Candidate: Serial number: When establishing a connection for the first time, both the client and the server randomly initialize a serial number for this connection. (Throughout the TCP process, serial numbers can be used to solve the problem of out-of-order network packets.)
Candidate: Acknowledgement number: This field indicates that the “receiver” tells the “sender” that the previous packet has been successfully received (acknowledgement number can be used to solve the problem of network packet loss)
Candidate: And tag bits are easy to understand. When SYN is 1, you want to create a connection. When ACK is 1, the confirmation number field is valid. If FIN is 1, the connection is disconnected. If RST is 1, the TCP connection is abnormal and needs to be disconnected.
Candidate: Let’s start with the three-way handshake, and I’ll also mention the TCP state involved in the three-way handshake.
Candidate: The TCP three-way handshake is all about confirming the serial numbers of the two parties (client and server)
Candidate: Here’s how it works
Candidate: At the beginning, both the client and the server are in CLOSE state
Candidate: The server actively listens to a port and is in LISTEN state
Candidate: The client randomly generates the sequence number (usually called client_ISN), sets the flag bit to SYN (meaning to connect), and sends the message to the server
Candidate: After the client sends the SYN packet, the client enters the SYN_SEND state
Candidate: After the server receives the client request, it also initializes the corresponding sequence number (typically called server_ISN)
Candidate: Enter client_ISN + 1 in the “Confirmation number” field and light both the SYN and ACK bits (set to 1)
Candidate: Sends the packet to the client, and the server state changes to SYN-REVD
Candidate: After receiving the packet from the server, the client knows that the server has received its serial number (known by the confirmation number) and received the serial number (server_ISN) from the server.
Candidate: At this point, the client needs to tell the server that it received the sequence number it sent, so fill in the “confirmation number” field with server_ISN +1, and mark the ACK bit as 1
Candidate: The client enters the ESTABLISHED state after sending packets, and the server enters the ESTABLISHED state after receiving packets from the client
Candidate: This is the three-way handshake process and the TCP state involved
Candidate: In summary, both parties send their serial numbers to each other to see if they can receive them. If “yes”, the communication is normal. (The three-way handshake shows that both parties have the ability to send and receive.)
Interviewer: How about two handshakes?
Candidate: The two-handshake only ensures that the serial number of the client is successfully received by the server. However, the server cannot confirm whether its serial number is successfully received by the client. So no (:
Interviewer: I see. Why is the serial number random? And how was the serial number generated?
Candidate: The random ISN is used to prevent attacks on different networks. On the other hand, the communication parties can discard packets that do not belong to the connection based on the sequence number
Candidate: How is the serial number generated? This is… Guess next should be with “clock” and TCP header properties to do the operation generated bar, similar to the snowflake algorithm (: SPECIFIC I forget.
Interviewer: Since the Internet is unreliable, wouldn’t it take three handshakes to establish a connection? Well, what if it gets lost on the way?
Candidate: Suppose the first packet is lost, and the server loses the SYN packet sent by the client.
Candidate: If the client fails to receive the ACK packet from the server, the client periodically retransmits the ACK packet until it receives the ACK packet from the server
Candidate: If the second packet is lost, the server’s SYN ACK packet is lost.
Candidate: The server fails to receive the ACK packet from the client and retransmits the PACKET periodically until it receives the ACK packet from the client
Candidate: Suppose the third packet is lost (ACK packet). After sending the third packet, the client enters the ESTABLISHED state unilaterally. The server also thinks that the connection is normal, but the third packet does not reach the server
Candidates: If the server and client do not send any SYN ACK packets, the server considers that the SYN ACK packet sent by the server has not been sent to the client, and therefore retransmits its SYN ACK packets due to timeout
Candidates: 2. If the client is about to send Data and the server receives the ACK + Data packet, it automatically switches to the ESTABLISHED state and receives the Data packet from the client
Candidates: 3. If the server tries to send data, but fails to send it, it periodically retransmits SYN + ACK until it receives the ACK packet from the client
Interviewer: Well, are we going to talk about four waves?
Candidate: Well, after the connection is ESTABLISHED, both the client and the server are in the ESTABLISHED state
Candidate: Both parties have a right to disconnect, so I’ll use the client as an example.
Candidate: The client intends to close the connection and sends a FIN packet to the server. After the client sends the packet, it enters the FIN_WAIT_1 state
Candidate: After receiving a FIN packet, the server replies an ACK packet to the client, indicating that the client has received the PACKET. After the server sends the ACK packet, the server enters CLOSE_WAIT state
Candidate: After receiving the ACK packet from the server, the client enters the FIN_WAIT_2 state
Candidate: The server may have data to send to the client. After confirming that it has no data to send to the client, the server sends a FIN packet to the client and enters the LAST_ACK state
Candidate: After receiving the FIN packet from the server, the client responds to the ACK packet and enters the TIME_WAIT state
Candidate: The server enters the CLOSE state after receiving the ACK packet from the client
Candidate: The client waits for 2MSL in TIME_WAIT and also enters the CLOSE state
Candidate: This is the end of the four-way wave flow, combined with the three-way handshake, the various TCP states are said.
Interviewer: Yeah, four waves. Why do you think it was four waves?
Candidate: After the client sends a FIN packet for the first time, it indicates that the client stops sending data to the server, but the client can still receive data. After receiving a FIN packet, the server may need to transmit data to the client. Therefore, it can only reply an ACK packet to the client first
Candidate: The server sends a FIN packet to the client when no more data is sent to the client.
Candidate: So, once, once, four times.
Interviewer: From the four-wave process, there is a TIME_WAIT state. Do you know what this state is for? (Wait 2MSL)
Candidate: There are two main reasons. 1. Ensure that the receiving party can receive the final ACK packet. (If the receiving party cannot receive the ACK packet, it resends the FIN packet.) 2. Ensure that any residual data from the previous network is lost when a new connection is created
Candidate: Actually also relatively easy to understand. Just like when we restart a server, we will gracefully shut down all resources and leave a period of time, during which time we hope the resources are normally shut down, so that restarting the server (or publishing) will basically not affect the online operation.
Interviewer: What’s the harm of assuming more than one TIME_WAIT state? How do you solve it?
Candidate: From a workflow perspective, the TIME_WAIT state occurs only on the party that initiates the closing of the connection. The harm is that it will occupy memory resources and ports (after all, it is waiting), solve the problem, there are Linux parameters can be set, specific forget the amount.
Interviewer: One last question for today. We talk about a TCP connection. What is this connection? What do you make of that?
Candidate: As you can see from the three-way handshake, TCP establishes a connection by exchanging states (such as serial numbers). And then there was no… Connections are essentially “just maintaining a state with each other, with connection properties”
Interviewer: Ok.
Follow my wechat official account [Java3y] to talk about something different!
Online Interviewers + Write Java projects from scratchContinuous high intensity update! O star!!!!!
Original is not easy!! Three times!!