This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Rest cannot be enjoyed by lazy people~

Previously: The extremely smart interviewer continued the interview with Xiaozhuang, let’s watch together

Interviewer big guy :(face with kindness) young man, come, let’s continue to chat, today to talk about the network of this thing ~

Me :(always ready) you ask ~

Interviewer: What’s the difference between an HTTP get request and a POST request?

Me: The main feature of a GET request is to obtain resources from the server. During the process of sending the request, the parameters of the request are spelled after the URL. A POST request is usually used for form submission, which is equivalent to submitting information to the server and waiting for the server to respond. In POST mode, the parameters submitted to the back end are placed in the body of the request. There is no requirement on the length of the parameters to be carried. In the process of sending a GET request, one TCP packet is generated, while in the process of sending a POST request, two TCP packets are generated. For a GET request, the browser sends the HTTP header and data together, and the server responds with status code 200 to return the resources required by the client. For a POST request, the browser sends header and the server responds with a status code of 100, which means continue. The browser then sends data and the server responds with a status code of 200.

Interviewer: THE TCP protocol, you know, briefly talk about the TCP protocol of three handshakes and four waves?

Me: TCP protocol is the protocol of transport layer, based on the communication of TCP protocol client and server must establish a bidirectional communication connection, after the establishment of the connection can send data, three handshake and four wave corresponding to the TCP connection and release process.

When establishing a TCP connection, the client first sends a connection request to the server. After receiving the request, the server informs the client that it has received the request and sends a connection request to the client at the same time. Then the client informs the server that it has received the request.

In a TCP connection is disconnected, by which one can, for example the client want to disconnect, the client send want to disconnect the request to the server, the server receives the request after will tell the client has received the request, but not immediately interrupt service end connected to the client, because this time may be a client needs to data has not been finished, After the data transmission from the server to the client is completed, the server will send a disconnection request to the client, and the client will confirm the disconnection information to the server after receiving the request. At this point, the TCP connection is disconnected.

Interviewer boss: You know the sticky bag problem?

I: The sticky packet problem is the problem in the transmission of TCP protocol, which is a flow-oriented protocol. The main reason for the sticky packet problem is that the receiver does not know the boundary between messages and does not know how many bytes of data to be extracted at a time.

The underlying communication between the sender and the receiver is still based on the operating system. As the operating system has its own mechanism to specify when to send data to the receiver, the same service data may be divided into multiple packets or multiple service data may be packaged into the same packet for sending.

Since the root of the problem is that the receiver does not know the boundaries between messages, the solution to the sticky packet problem is to let the sender inform the receiver of the total size of the data stream before sending data, and then the receiver can start a loop to receive all the data.

Interviewer: do you know about cookies and sessions?

Each request is independent, and the data generated by the client service during the second session will not be retained. However, with the development of the Internet, it is necessary to save the state. In order to solve the stateless nature of THE HTTP protocol, cookie appears. Cookie specifically refers to a small piece of information stored in the client browser. When the user logs in for the first time, the server will generate a random string and save it in the client browser. The next time the client browser accesses the server again, it will automatically carry this string, and the server can obtain the user’s information on this string.

However, since cookies are stored on the client, which may be intercepted or stolen, there are great security risks. Therefore, a new technology is needed to save user information on the server to ensure the security of information, namely session. Session is based on the cookie, the session is stored in a random string from the server, when it is the first time you login the server will generate a random string stored in the client browser, next time the client access server, will carry the random string, the service side by the client to carry data to compare the server database, Check the current login user is legal, but if there is a lot of user login, can produce a lot of data, when a user login server to query the database will be very slow, and if adopt distributed services, you need in each server configuration the same database, because when use nginx forwarding, You do not know which server the request will be forwarded to, which will take up a lot of server resources, token can solve this problem. The first part is the header, the second part is the load, and the third part is the signature. The first part stores the encryption method/company information after base64 transcoding, and the second part of the load stores the user-insensitive information after Base64 transcoding, such as the user name expiration time. The third part is the signature obtained by the combination of the first two parts after transcoding and hash encryption. When it is the first time you login, the server will return to such a random string to the client browser save, next time again want to access server for other operation, the server will be through their own way to decoding the former two parts, the random string is obtained by the same way of encryption and the client browser with string comparisons, judge the user information is correct. The advantage of using the token is that the server does not need to store user data and saves server resources. Each server only needs to save the corresponding method.

Interviewer boss: 666, what happens when you type in the URL in the address bar and press Enter?

I: First, the browser analyzes the URL that the link points to the page, and then requests the DNS to resolve the IP address of the target URL. After the DNS resolves the IP address of the URL, the browser establishes a TCP connection with the server. After the connection is established, the browser will send HTTP requests to the target server for data packets. If the target server is a simple page, it is returned directly. For pages that require redirection, the browser gets the redirection response, finds the redirection address, and repeats the first step. The browser then resends the request with the new URL, and the target server returns the data.

Interviewer boss: I basically don’t have any problems, wait for the next interviewer to talk to you ~

Me: Ok