Pre –

HTTP: Hyper Text Transfer Protocol (HTTP) is a simple request-response Protocol that usually runs on top of TCP.

2, TCP6 flag bit SYN(synchronous establish online), ACK(Acknowledgement), PSH(push transmission), FIN(Finish end), RST(reset reset), URG(Urgent Urgent)

Three-way handshake

Let’s take (Lu Ban) calling (Master Lu Ban) as an example

Master Lu Ban: Hello, how are you? SYN (seq = x)? Master Lu Ban ->> Master Lu Ban: ACK (ACK = x + 1) SYN (seq = y) Hello, can you hear me? Master Lu Ban: ACK(ACK = y + 1) I can hear you!

parsing

Here (Lu Ban) is our client, (Lu Ban master) is the server

  • First handshake

Lu Ban took out a mobile phone (here on behalf of the client machine), dialed master Lu Ban’s phone (here on behalf of the server) in three dudu dudu, said a hello? He then threw a SYN packet containing a Synchronize Sequence number (seq=x), set the phone to SYN_SENT and waited for Master Lu Ban to reply.

  • Second handshake

At this moment, master Lu Ban’s phone rang, answered the call, and he hit lu Ban’s bag in the face. When he opened the bag, he saw that there was an “X” inside. According to his years of experience, an ACK packet was immediately generated with an Acknowledge number (ACK = X + 1). A SYN packet is also generated and placed in (seq = y), then both packets are thrown to Lu Ban, and the phone is happily set to SYN_RECV, waiting for Lu Ban to respond.

  • Third handshake

After waiting for a while, Lu Ban received two packets thrown by Master Lu Ban. When he opened the packets, he saw ACK (ACK = x + 1) SYN(seq = y). The clever Lu Ban took the cue and also threw out ACK (ACK = y + 1) packets. After the client and server enter ESTABLISHED, the TCP connection is successful and the three-way handshake is complete.

Note: many of the above scenarios are purely destruction. they are just for easy understanding and memorization. please refer to the principle

SequenceDiagram Client ->> Server: SYN (SEq = X) Server ->> Client: ACK(ACK = X + 1) SYN (SEq = Y) Client -) Server: ACK(ACK = y + 1)

At this time the partner put forward a question, why must three times, two times not fragrant?

In our opinion, two is fine, but for safety reasons, the third handshake is very important.

The first handshake, the server knows that the client is the second handshake that can send a message, the client knows that the server is the third handshake that can connect, and the server knows very clearly that whatever I send to the client, the client can receive

If did not shake hands for a third time, the second handshake is completed, the server is sent directly to the client, but unfortunately, at this time the client has disconnected the connection, the server does not know, still send something to the client, the client must be unable to receive the service side of things, thus, three-way handshake is indispensable

Four times to wave

Scene reappearance

After a while, Lu Ban had nothing more to say to Master Lu Ban and decided to hang up

FIN (x), Lu Ban master ->> Lu Ban: Oh ACK (x + 1) lu Ban Master ->> Lu Ban master: Oh ACK (y + 1)

parsing

  • First wave

Lu Ban said to Master Lu Ban, “THAT’s all.” And send a FIN (X) notification

  • Second wave

Upon receiving the FIN (X) notification, Master Luban said “Oh” and sent an acknowledgement packet to Luban ACK (x + 1).

  • Third wave

Master Lu Ban remembered that he had to tell Lu Ban that he had nothing more to say, so he said to lu Ban, “I have finished, too.” And send a FIN (Y) notification to Lu Ban

  • Fourth wave

Upon receiving the FIN (Y) notification, Luban sends the acknowledgement ACK (Y + 1) to Master Luban and disconnects

At this point, the four waves are over, and the wave picture is attached

SequenceDiagram Client ->> Server: FIN (X), Server ->> Client: ACK (X + 1) Server ->> Client: FIN (Y) Client ->> Server: ACK (Y + 1)