preface

About the network, I think you big brothers are both love and hate

What I love is that various powerful technical frameworks have encapsulated the network in a simple and easy to use, every day request, fetchData use fly

I hate being asked in every job 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 three times for two?”

“Why not wave four times out of three?”

“Why wait for 2MSL when waving 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 this every time I request data? This interview is really a headache

Don’t be afraid! In order to let brothers can once and for all, refuse to memorize! Little brother I combed this not afraid to forget the network basic knowledge guide, for everyone to taste ~

What are HTTP and TCP?

First of all, we need to know that these are both protocols, the full names are Http and TCP/IP

An agreement is what both sides agree on. Let’s say I make a deal with you, I give you an apple, you give me a banana. Apples for bananas is the details of our agreement

So there’s nothing mysterious about HTTP and TCP per se, just some specification for network requests.

So the question is, since these are protocols, how are they different?

There are of course many specific differences. But if you are not a professional network technician, you just need to have an overall understanding. This is the time to bring up the teacher taught us TCP/IP layered model diagram.

Hee hee, familiar with? Here we only need to remember the application layer and transport layer, the rest is rarely used

From this model, we can know that network request is a very complex process, so complex that it must be divided into several layers, and each layer can complete its task well before the request can be successful

The transport layer’s job is to establish network connections

The application layer is the transfer of various types of data over established network connections. Text, images, video, etc

Since network request is so complicated, 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 big difference between HTTP and TCP

Socket

At this point, the concept of socket is introduced

What is a socket?

We know from the previous, HTTP and TCP are only protocols, protocol itself is not the ability to achieve functions, need a carrier to achieve it.

Socket is the implementation of TCP protocol.

It defines interfaces for establishing connections. For example, connect, Listen, read and so on, the implementation of these interface methods strictly follow the TCP protocol

Okay, now that we know what it is, what’s the highlight of the interview

Three handshakes and four waves

Let’s first make it clear that the three-way handshake and four-way wave are the content of TCP/IP protocol, which belongs to the transport layer and is the way that network requests establish and disconnect connections while transmitting data

How do I set up a network connection

Before we look at what a three-way handshake is, let’s ask the question, okay

Now there are client and server, how do we set up a network connection?

The easiest thing to do, of course, is for the client to send a request to the server to establish a connection, and then ignore it. I pretend that the connection has been established, and happily wait for the server to send me the data

Is that ok?

Of course not, man.

What if the server isn’t even on? What if they don’t even talk to you

It’s like writing a love letter to your sister, and it only works when your sister responds. You write to her regardless of whether she responds or not, and you wait there stupidly.

That’s not love. That’s harassment and back-up

So we have a basic rule for making connections

“Requests must be answered”

Keep going. Since there is a response, my client sends a request to you, and then sends another request after receiving the response from the server. Does it seem very orderly?

It’s you. It’s the “Stop waiting agreement.”

Timeout retransmission

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

Simplicity means problems because the network environment is unstable

For example, each request from the client must wait for a reply to the last request. If the last request is delayed or simply dropped due to network fluctuations, the next request from the client will never be sent. This is where the data is stuck?

To solve this problem, TCP has a timeout retransmission mechanism. If no reply is received for a long time, the original packet is sent again. I haven’t received it and will send it again until it’s normal.

The serial number

Now that we understand that when a network connection is established, “requests must be answered” and “timeout retransmission”, let’s consider another problem

We know that the data transmitted by connection requests is in the form of packets. That is, the data in a request is made up of multiple packets.

If a packet is delayed due to network fluctuation, and a timeout retransmission is triggered during the delay, then the server receives two identical packets, what does the server do?

The way to deal with it is simple. When a client sends a packet, it adds something to it so that the server can tell where the packet is coming from

The process is to number the packets. This number is called the serial number of the packet and is the key to the next three handshakes

Three-way handshake

Finally, three handshakes, so why don’t you guys give us a “like” for a brain change?

So let’s start with a simple premise

A three-way handshake is essentially a four-way handshake, just a combination of the two middle handshakes.

Because the connection is continuous. The server sends a response as soon as it receives the request from the client in the first handshake, and sends its own request, so it combines the response and request into a single request, the second handshake

Why three handshakes instead of two?

So the question is, according to the above basic principle of “request must be answered”

Why three handshakes instead of two?

In the fourth edition of Computer Network written by Xie Xiren, it is stated that the purpose of “three-way handshake” is “to prevent the invalid connection request message segment from being suddenly transmitted to the server and causing errors”.

There is no problem with this sentence as an interview. But there’s a key reason that’s been skipped

Remember we said why packets have serial numbers?

To enable the server to identify the source of the packet and determine whether the packet is outdated.

Let’s go from a packet to a network connection. If a packet is delayed for a long time until the current connection is disconnected and the next connection is established, how can the server distinguish between the two connections?

The answer is to add another serial number

This sequence number is called the initial sequence number and identifies a connection.

So the conclusion is pretty clear.

The initial serial number is used to identify whether the connection is the same, and the packet serial number is used to identify the source of packets in a connection.

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

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

Ha, did you forget TCP is full duplex communication? Client and server concepts are relative, gender swap, mother does not love

The initial serial number sent from 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 to the other party. Add that to the handshake merger, and you’ve got three handshakes

Did you see the light? Hey hey

Here, check yourself out and ask yourself as if you’re the interviewer

“Why three handshakes and not four handshakes?”

“Why three handshakes instead of two?”

Four times to wave

Halfway through the long march, four waves of disconnection

In fact, the four waves are similar to the three handshakes, except that the second and third waves cannot be combined

Why is that?

The server must immediately respond to the first handshake request and send the request simultaneously. However, because TCP is full-duplex communication, the server may need to send data after the client disconnects.

So a “request-response” by the client only disconnects the client -> server, and the server sends a request to disconnect the server -> client after a while

Why wait for 2MSL

The client will wait 2msL (the maximum lifetime of a message on the network) to shut down completely after the last wave of a response.

It’s the network instability. If the reply packet sent by the last wave of the client is delayed or lost, the server will wait for one MSL to receive it and retry it, and repeat the process of the third wave.

The client will receive a disconnection request at most 2MSL, so wait at least 2MSL before disconnecting

Ok, that’s most of the information about HTTP and TCP. Of course, there are many areas that can be expanded, such as the “timeout retransmission” area, to solve the performance problems of continuous transmission, sliding Windows, congestion control and so on. However, these are the branches of HTTP knowledge, as long as the main knowledge of the rest of the branches are not easy to grasp

Of course, the interviewer will also ask some simple memorized questions, such as:

“Tell me the difference between Http1.0, Http1.1 and Http2.0.”

The differences between Http1.0, Http1.1 and Http2.0

Http1.1 and 2.0 must be optimized.

Http1.1 is optimized for long connections and is currently the most widely used Http protocol.

The idea of a long connection is simple. In Http1.0, we each request a picture to establish a Tcp connection, request a web page often dozens of times Tcp connection, disconnect, this is very time-consuming, long connection means that multiple requests can reuse a Tcp connection, improve the utilization of Tcp connections

If the interviewer asks you if you have more, tell them you have more

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 entire video first. Http1.1 optimizes this funny scenario by adding a range to the request header so that only a portion of the data can be requested

If ask you still have….

You say commonly used almost these, not I go back to have a good look, and so we do a colleague I say good with you..

Http2.0 is even better

It supports multiplexing. Previously, Http1.1 was just about reusing Tcp connections. Http2.0 can request multiple resources on a single request

This is similar to combining two requests into one. The server returns twice requested resources at once. Amazing

To be continued

I’ve written so much before I know it. Speak out the knowledge as popularly as possible, is to be able to give you a little bit of understanding to help, do not have to back the eight-part essay every time painful. There is also a sort of HTTPS, limited to space in the next article ~~

I am the wood

A worker struggling to make his way up in the Internet world

Try to live, try to move forward

If you feel good, please follow me and I will continue to share something valuable to you

Welcome to follow my public account @Fangmu Rudy