Easy to understand, read the network interview knowledge points that will not be forgotten!!

Time:2021-9-8

preface

On the Internet, I think you big brothers love and hate

What I love is that various powerful technical frameworks have already encapsulated the network, which is simple and easy to use. Every day, requests and fetchdata fly up

Hate to be asked at every interview:

“What’s the difference between HTTP and TCP?”

“What is the difference between HTTP and socket?”

“What is three handshakes and four waves?”

“Why not shake hands twice and shake hands three times?”

“Why not wave three times and wave four times?”

“Why wait 2msl?”

“What’s the difference between http1.0, HTTP1.1 and http2.0?”

“What’s the difference between HTTP and HTTPS?”

….

??? Do I have to recite these silently every time I request data? This interview stereotyped article is really a headache

don ‘t panic! In order to let the brothers once and for all, refuse to memorize by rote! Little brother, I combed this unforgettable guide to basic network knowledge for you to taste~

What are HTTP and TCP?

First of all, we should know that these two protocols are called HTTP protocol and TCP / IP protocol

The agreement is the content agreed by both parties. For example, I agreed with you that I would give you an apple and you would give me a banana. Apple for banana is the specific content of our agreement

So HTTP and TCP are not mysterious in nature,It only makes some specifications for network requests

So the question is, since they are all agreements, what is the difference between them?

Of course, there are many specific differences. But if you are not a professional network technician, you only need to have an overall understanding. At this time, we need to move out the TCP / IP layered model diagram taught by our teacher.

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Hee hee, are you familiar with it? Here we just need to rememberapplication layerandTransport layerIt’s OK. Other basic functions are rarely used

From this model, we can know that network request is a very complex process. It is so complex that it must be divided into several layers. Only when each layer completes its own task can the request succeed

Transport layerYour task is to establish a network connection

application layerIs to transfer various types of data on the established network connection. Such as text, image, video and so on

Since the network request is so complex, it is impossible to expect one protocol to describe the whole network request process

So HTTP corresponds to the application layer protocol and TCP corresponds to the transport layer protocol

This is the biggest difference between HTTP and TCP

Socket

This leads to a concept, socket

What is socket?

As we know from the above, HTTP and TCP are just protocols. The protocol itself does not have the ability to realize functions. A carrier is needed to implement it.

Socket is the implementation of TCP protocol.

It defines some interfaces for establishing connections. For example, connect, listen, read, etc. the specific implementation of these interface methods strictly follows the TCP protocol

OK, I know what it is. Next is the highlight of the interview

What are three handshakes and four waves?

We first make it clear that three handshakes and four waves are the contents of TCP / IP protocol. They belong to the transport layer and are the way for the network to establish and disconnect when requesting data transmission

How to establish a network connection

Before we know what three handshakes are, let’s explore a problem

Now there are client and server. How can we establish a network connection?

The simplest thing is, of course, that the client sends a request to the server to establish a connection, and then I don’t care. I just wait in place when the connection has been established, waiting for the server to send me data

Is this ok?

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Of course not, brother

What if the server doesn’t start at all, and people ignore you

It’s like writing a love letter to your sister. It’s only when your sister responds to you. Whether your sister responds or not, you write to others and wait there foolishly.

It’s not love, it’s x harassment and spare tire

So we come up with a basic criterion for establishing connections

“Request must have a response”

Keep thinking. Since there should be a response, my client will send you a request, and then send the next request after receiving the response from the server. In turn, does it seem very orderly?

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Worthy of you, this is “stop waiting for the agreement”

Timeout retransmission

“Stop waiting” is the simplest version of a network request

Simplicity means problems, because the network environment is unstable

For example, every request from the client will not be sent until the reply to the previous request is received. If the previous request is delayed or simply lost due to network fluctuations, won’t the next request from the client be sent forever? The data is stuck here?

In order to solve this problem, there is a timeout retransmission mechanism in TCP protocol. If no reply is received for a long time, the original packet will be sent again. It hasn’t been received and sent again until it’s normal.

Easy to understand, read the network interview knowledge points that will not be forgotten!!

serial number

Understand that when a network connection is established, “a request must have a response” and “timeout retransmission”. Next, let’s think about another problem

We know that the data transmitted by the connection request is transmitted in the form of data packets. That is, the data requested at one time is composed of multiple data packets.

If a packet is delayed due to network fluctuation and timeout retransmission is triggered during this delay, the server will receive two identical packets. How will the server handle it?

Easy to understand, read the network interview knowledge points that will not be forgotten!!

The process is simple. When the client sends a packet, it adds something to it so that the server can identify where the packet comes from

This process is to number the packets. This number is called packetserial number, this is the key to the next three handshakes

Three handshakes

Finally, I shook hands three times. Why don’t you give me a compliment and change your mind?

Let’s start with a simple premise.Three handshakes are actually four handshakes, which just combines the second handshake and the third handshake

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Because the connection is continuousAfter receiving the client’s request in the first handshake, the server should immediately send a response and send its own request, so the response and request are combined into one request, that is, the second handshake

Why not shake hands twice but three times?

So here comes the question. According to the basic principle of “request must have response” above

Why not shake hands twice but three times?

In the fourth edition of computer network written by Xie Xiren, it is said:

The purpose of “three handshakes” is “to prevent the invalid connection request message segment from being suddenly transmitted to the server, resulting in errors”

There’s no problem memorizing this sentence as an interview stereotype. But in fact, a key reason is skipped

Remember why we said the packet should have a serial number?This is to enable the server to identify the source of the data packet and whether it is an outdated data packet.

We move from packets to a network connection. If a packet is delayed for a long time until this connection is disconnected and the next connection is established, then the server receives the packet of the old connection. How to distinguish these two connections?

The answer is to add another serial number

This serial number is called the initial serial number and is used to identify a connection

So the conclusion is very clear.The initial serial number is used to identify whether it is the same connection, and the packet serial number is used to identify the source of the packet in a connection

Therefore, the client needs to send the initial serial number representing the local connection to the server

Wait, you still don’t understand why it’s three handshakes, not two?

Ha, have you forgotten that TCP protocol is full duplex communication? The concepts of client and server are relative. Gender is interchangeable and parents don’t love each other

The initial serial number sent by the client to the server is the connection established by the client. The server can also be the client and send the initial serial number to the other partyPlus the handshake merger mentioned before, it’s three handshakes

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Suddenly realized? hey

Come on, check yourself and ask yourself as an interviewer

“Why three handshakes instead of four”

“Why three handshakes, not two”

Four waves

Halfway through the Long March, let’s watch the four waves of disconnection

In fact, four waves are almost the same as three handshakes. The difference is that the second and third waves cannot be combined

Why?

The reason why three handshakes can merge isThe connection is continuous, after receiving the first handshake request, the server should respond immediately and send the request at the same time. But because TCP is full duplex,After the client is disconnected, the server may also need to send data.

Therefore, the client needs to do it once"Request response"It’s just disconnectedClient -- > serverThe server sends a request to disconnect after a whileServer -- > clientConnection of

Why wait for 2msl

Here’s a little bit of knowledge,After the client sends a response with a wave for the last time, it will wait for 2msl (MSL is the maximum lifetime of a message on the network) before it is completely closed. Why?

Or the problem of network instability. If the response message sent by the client’s last wave ④ is delayed or lost, and the server waits for an MSL, it will time out and retransmit, repeating the process of the third wave.

In this way, the client will receive the request to disconnect at most 2msl, so it needs to wait at least 2msl before disconnecting

Easy to understand, read the network interview knowledge points that will not be forgotten!!

Well, I’ve finished most of the knowledge points of HTTP and TCP. Of course, there are many places that can be expanded, such as “timeout retransmission”, which is introduced to solve the performance problemContinuous transmission, sliding window, congestion controlwait. However, these branches of HTTP knowledge are not easy to catch as long as they master the main knowledge

Of course, the interviewer will also ask some questions that can be answered by simple memory, such as:

“Tell me the difference between http1.0, HTTP1.1 and http2.0”

Differences between http1.0, HTTP1.1 and http2.0

What we said just now is actually the content of http1.0. I think HTTP1.1 and 2.0 must be optimized with my fingers.

The optimization of HTTP1.1 is mainly realizedLong connectionAt the same time, it is also the most widely used HTTP protocol.

The meaning of long connection is very simple. In http1.0, every time we request a picture, we need to establish a TCP connection. Requesting a web page often has dozens or hundreds of TCP connections and disconnects. This is very time-consuming. Long connection means that multiple requests can reuse a TCP connection, which improves the utilization of TCP connections

Easy to understand, read the network interview knowledge points that will not be forgotten!!

If the interviewer asks you what else you have, tell him what else you haveBreakpoint continuation

This is easy to understand. In http1.0, if we want to know the data in the middle of a video, we must download the whole video first. HTTP1.1 optimizes this Teaser scenario by adding range to the request header, which can only request part of the data

If I ask you, what else

You say that there are almost all the commonly used ones. No, I’ll go back and have a good look. I’ll tell you when we become colleagues

As for http2.0, it’s even more awesome

It supportsMultiplexing。 Previously, HTTP1.1 only multiplexes TCP connections. Http2.0 can request multiple resources on one request

Easy to understand, read the network interview knowledge points that will not be forgotten!!

It is similar to merging two requests into one request. The server returns the requested resources twice at a time. Amazing

Unfinished to be continued

Unknowingly, I have written so much. Write my understanding as popular as possible to facilitate everyone’s memory. There’s more abouthttpsI’ll put it in the next article due to space limitation~~

I’m Fang Mu

A migrant worker struggling in the Internet world

Live hard and move forward

If you think it’s not bad, welcome to “pay attention to me” official account @ Fang Mu Rudy.

I will continue to share some valuable things with you~

Easy to understand, read the network interview knowledge points that will not be forgotten!!