On the Internet, I think you brothers love and hate each other
What I love is that all kinds of powerful technical frameworks have already encapsulated the network, which is simple and easy to use
I hate to be asked every time I have an interview
“What’s the difference between HTTP and TCP?”
“What’s the difference between HTTP and socket?”
“What is three handshakes, four waves?”
“Why not shake hands twice and three times?”
“Why not wave three times and wave four times?”
“Why wait for 2msl when you wave four times?”
“What’s the difference between http1.0 HTTP1.1 http2.0?”
“What’s the difference between HTTP and HTTPS?”
？？？ Do I have to recite them every time I ask for data? This interview essay is really a headache
don ‘t panic! In order to let brothers once and for all, refuse to rote! Little brother, I have combed this unforgettable guide to basic network knowledge for you to taste~
What are HTTP and TCP?
First of all, we need to know that these two are protocols. The complete name is HTTP protocol and TCP / IP protocol
An agreement is what both parties agree on. For example, I have an agreement with you that I will give you an apple and you will give me a banana. Apple for banana is the specific content of our agreement
So there’s nothing mysterious about HTTP and TCP,It just makes some specifications for network requests
So the question is, since they are all agreements, what’s 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 that our teacher taught us.
Hee hee, familiar? Here we just need to rememberapplication layerandTransport layerIt’s OK. The others are rarely used
From this model, we can know that network request is a very complex process, which must be divided into several layers, and each layer can successfully request only after completing its own task
Transport layerOur task is to establish a network connection
application layerIt is to transfer all kinds 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 describe the whole network request process with one protocol
So HTTP corresponds to the application layer protocol, TCP corresponds to the transport layer protocol
This is the biggest difference between HTTP and TCP
This leads to the concept of socket
What is socket?
As we know, HTTP and TCP are just protocols. The protocol itself does not have the ability to realize functions. We need a carrier to realize it.
Socket is the implementation of TCP protocol.
It defines some interfaces for establishing connections. For example, connect, listen, read and so on, the specific implementation of these interface methods strictly follow the TCP protocol
OK, I know what it is. The next step is the interview
What is three handshakes and four waves?
First of all, we make it clear that three handshakes and four handshakes are the contents of TCP / IP protocol. They belong to the transport layer and are the ways for network requests to establish and disconnect connections when transmitting data
How to establish a network connection
Before we know what a triple handshake is, let’s talk about a problem
Now there are client and server. How can we establish a network connection?
The simplest way is, of course, for the client to send a request to the server to establish a connection, and then ignore it. I will treat the connection as established and wait for the server to send me data
Is this ok?
Of course not, man
What if the server doesn’t turn on at all? What if people ignore you
It’s like writing a love letter to your sister. It’s only when your sister responds to you that it’s funny. You don’t care whether your sister will reply or not, write to others and wait there.
It’s not called love. It’s x harassment and spare tire
So we come up with a basic rule for connecting
“Request must have response”
Keep thinking. Since there should be a response, then my client will send you a request. After receiving the response from the server, I will send the next request. Is it very orderly to proceed in turn?
It’s you. That’s the “stop waiting agreement.”
“Stop waiting” is the simplest version of a network request
Simplicity means problems, because the network environment is unstable
For example, every request of the client will not be sent until it receives a reply from the previous request. If the previous request is delayed or lost because of network fluctuations, won’t the next request of the client never be sent out? Is the data stuck here?
In order to solve this problem, there is a timeout retransmission mechanism in TCP protocol. If there is no reply for a long time, the original packet will be sent again. It hasn’t been received and sent again until it’s normal.
I understand that when a network connection is established, “the 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 packets. In other words, the data requested at one time is composed of multiple packets.
If a packet is delayed due to network fluctuation and a timeout retransmission is triggered during the delay, then the server will receive two identical packets. What should the server do?
The way to deal with it is very simple. When the client sends a packet, it adds something to it, so that the server can identify where the packet comes from
The process is to number packets. This number is called packet numberserial numberThis is the key to the next three handshakes
Finally, it’s time to shake hands for three times. Would you like to give a compliment and change your mind?
Let’s start with a simple premise.The essence of three handshakes is actually four handshakes, which is just a combination of the second handshake and the third handshake
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 at the same time, so it combines the response and request into one request, that is, the second handshake
Why not two handshakes but three?
So here comes the question, according to the basic principle of “request must have response” above
Why not two handshakes but three?
In the fourth edition of computer network written by Xie Xiren, it is said that:
The purpose of “three times handshake” is “to prevent the invalid connection request message segment from suddenly being sent to the server, resulting in errors”
It’s no problem to recite this sentence as an interview essay. But in fact, a key reason has been skipped
Remember why we said packets should have serial numbers?It is to enable the server to identify the source of the packet and whether it is an outdated packet.
We’re going from a packet 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 can we distinguish the two connections?
The answer is to add a 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 sequence number is used to identify whether it is the same connection, and the packet sequence number is used to identify the source of the packet in a connection
Therefore, the client needs to send the initial serial number of the local connection to the server
Wait, you still don’t understand why it’s three handshakes, not two?
Ha, did you forget that TCP protocol is full duplex communication? The concept of client and server is relative, gender exchange, mother does not love
The initial serial number sent by the client to the server is the connection established by the client. The server can also act as the client and send the initial serial number of the connection to the other partyIn addition to the previous handshake merger, it’s three handshakes
Did you suddenly realize it? hey
Come on, check yourself out and ask yourself as an interviewer
“Why three handshakes, not four handshakes”
“Why three handshakes, not two?”
Halfway through the Long March, let’s look at the four waves of disconnection
In fact, four waves and three handshakes are almost the same, the difference is that the second and third waves can not be combined
What’s the reason that the three handshakes can be combinedThe connection is continuousAfter 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.
So the client can do it once
"Request response"It’s just disconnected
Client serverThe server will send a request to disconnect after a while
Server > clientConnection of
Why wait for 2msl
Here’s a little bit of knowledge,The client will wait for 2msl (MSL is the maximum survival time of a message on the network) time after the last wave to send a response. Why?
Or the instability of the network. If the response message sent by the client for the last time wave is delayed or lost, and the server waits for one MSL but does not receive it, it will retransmit over time and repeat the process of the third wave.
In this way, the client will receive the request to disconnect after 2msl at most, so it is necessary to wait for 2msl at least before disconnecting
Well, that’s all for HTTP and TCP. Of course, there are many places that can be expanded, such as “timeout retransmission”. In order to solve the performance problem, it introduces theContinuous transmission, sliding window, congestion controlwait. However, as long as we master the main knowledge, the other branches and leaves are not easy to grasp
Of course, the interviewer will also ask some questions that can be answered by simple memory, such as:
“Let’s talk about the difference between http1.0, HTTP1.1 and http2.0”
The difference between http1.0, HTTP1.1 and http2.0
What we have just said is actually the content of http1.0. I think HTTP1.1 and 2.0 must have been optimized.
The optimization of HTTP1.1 is mainly to realize theLong connectionIt is also the most widely used HTTP protocol.
The meaning of long connection is very simple. In http1.0, we need to establish a TCP connection every time we request a picture. There are dozens or hundreds of TCP connections and disconnects when we request a web page. This is very time-consuming. Long connection means that multiple requests can reuse a TCP connection, which improves the utilization of TCP connection
If the interviewer asks you if you have any, you can tell him if you have anyBreakpoint 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 scenario by adding a range to the request header to request only a part of the data
If you have any more
You said that the commonly used ones are almost the same. No, I’ll go back and have a good look. I’ll talk to you when we are colleagues
As for http2.0, it’s even more powerful
It supportsMultiplexing。 Previously, HTTP1.1 just multiplexes TCP connections. Http2.0 can request multiple resources on one request
It’s similar to merging two requests into one. The server returns two requested resources at a time. Amazing
To be continued
Unconsciously wrote so much, as far as possible popular to write out their understanding, convenient for everyone to remember. There’s more about it laterhttpsI’ll put it in the next chapter because of the limited space~~
I am 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~