Recently, Afen’s colleagues have been preparing for interviews, and some good ones have received offers. After all, the epidemic has stabilized. However, when Afen was asked about the HTTP protocol during the telephone interview, she seemed a little numb. Why? Because the routine was too complicated, which caught Afen off guard.
Interviewer: Do you understand TCP/IP protocol? To be honest, when Ah Fen heard this question, her first thought was, if I answer this question, there will definitely be a three-way handshake and four-way wave waiting for me, but I still have to answer it, so Ah Fen started to answer. Ah Fan began to answer: Although TCP/IP protocols are mentioned together, they are actually two different protocols.
Interviewer: Then tell me what is a three-way handshake and what is a four-way wave. 1. Three-way handshake Look at this picture. It comes from Baidu search, and there are all kinds of pictures on Baidu. When you see the picture at first, you will definitely not understand it, that is, you can only analyze it through the "line" of the symbol in the painting. In fact, this is only one aspect. Then let's analyze what this picture means based on the picture. There are two serial numbers and three different flags in the picture, some of which are easily confused by uppercase and lowercase. Serial number:
So how do we tell the interviewer about this picture? (1) First handshake (SYN=1, seq=x): The client sends a packet with the TCP SYN flag set to 1, indicating the port of the server to which the client intends to connect, and the initial sequence number X, which is stored in the Sequence Number field of the packet header. After sending, the client enters the SYN_SEND state. (2) Second handshake (SYN=1, ACK=1, seq=y, ACKnum=x+1): The server sends back an ACK packet. That is, the SYN flag and the ACK flag are both 1. The server selects its own ISN sequence number and puts it in the Seq field. At the same time, the Acknowledgement Number is set to the client's ISN plus 1, that is, X+1. After sending, the server enters the SYN_RCVD state. (3) The third handshake (ACK=1, ACKnum=y+1) The client sends an ACK packet again, with the SYN flag set to 0 and the ACK flag set to 1. It also adds 1 to the sequence number field of the ACK sent by the server, puts it in the confirmation field and sends it to the other party, and writes ISN + 1 in the data segment. After sending, the client enters the ESTABLISHED state. When the server receives this packet, it also enters the ESTABLISHED state, and the TCP handshake ends. If you say this, the interviewer may ask, this is too official, can you explain your understanding, then you can use a practical example to explain it to him. Ah Fen: Ji Ding, hey, I'm Ah Fen, can you hear me? Ji Ding: What are you arguing about? I heard you. Who else could I know besides you? Ah Fan: You can hear me, but you didn't reply quickly. No wonder you don't have a girlfriend. Let's continue our conversation. The three-way handshake process is commonly known as the three-way handshake, during which three conversations are conducted to determine whether the data transmission channels in both directions are normal. 2. Wave four times So how do you answer the four waves? (1) First wave (FIN=1, seq=x) Assuming that the client wants to close the connection, the client sends a packet with the FIN flag set to 1, indicating that it has no data to send, but can still receive data. After sending, the client enters the FIN_WAIT_1 state. (2) Second wave (ACK=1, ACKnum=x+1) The server confirms the client's FIN packet and sends a confirmation packet, indicating that it has received the client's request to close the connection, but is not yet ready to close the connection. After sending, the server enters the CLOSE_WAIT state. After receiving the confirmation packet, the client enters the FIN_WAIT_2 state and waits for the server to close the connection. (3) The third wave (FIN=1, seq=y) When the server is ready to close the connection, it sends a connection end request to the client with FIN set to 1. After sending, the server enters the LAST_ACK state and waits for the last ACK from the client. (4) The fourth wave (ACK=1, ACKnum=y+1) The client receives the close request from the server, sends a confirmation packet, and enters the TIME_WAIT state, waiting for a possible ACK packet requesting retransmission. After receiving this confirmation packet, the server closes the connection and enters the CLOSED state. After the client waits for a fixed period of time (two maximum segment lifecycles, 2MSL, 2 Maximum Segment Lifetime), it does not receive an ACK from the server, and thinks that the server has closed the connection normally, so it also closes the connection and enters the CLOSED state. After two retransmissions, it will retransmit until it times out. If there are too many, there will be a large number of half-connection blocking queues. So how do you explain this to the interviewer in a simple way? Ah Fen: Ji Ding, I have said everything I want to say, do you have anything else to say? Ji Ding: I understand what you said, but don’t stop. I still have something to tell you. Please find me a girlfriend. Ji Ding: xxxxx, I’m done. Ah Fan, that’s enough, stop talking. Remember, I’m hanging up. If the interviewer asks you this question and you answer it like this, with both the official explanation and your own understanding, then the question is almost answered. The interviewer will obviously not let you off like this, and will definitely give you another surprise: Why three handshakes instead of four waves? Why not three? This is because when the server is in the LISTEN state, after receiving the SYN message of the connection establishment request, it puts the ACK and SYN in one message and sends it to the client. When closing the connection, when receiving the other party's FIN message, it only means that the other party will no longer send data but can still receive data. Whether to close the data transmission channel now needs to be decided by the upper layer application. Therefore, the ACK and FIN of the server are generally sent separately. So at this time, the handshake is four times instead of three times. So how do we write an HTTP protocol by hand? Here is the code:
This is a simple implementation, just an implementation idea, not a real request processing. You should also pay attention to setting the Content-Type type, otherwise it is easy to go wrong, after all, the length is limited. |
>>: Learn crawling skills in Yiwen
Smart cities, smart grids, smart grazing/planting...
I shared information about SiliCloud in September...
While the Internet of Things is changing people’s...
GPP defines network slicing as one of the main fu...
CloudCone also released a special package for Chi...
To learn more about open source, please visit: ...
On July 14, Changting Technology, a provider of n...
"Smart Park" is not a new concept. In t...
[[382428]] From the perspective of technological ...
Three years after 5G was put into commercial use,...
CUBECLOUD has launched a promotion for Christmas ...
I encountered some problems when working on HTTPS...
It has been more than a year and a half since my ...
RAKsmart's independent server flash sale cont...
We say that there are many reasons for slow Inter...