preface

In our daily work, we contact HTTP requests all the time, and HTTP is based on TCP communication. In the interview, the interviewer will often ask us, do you know the three handshakes and four waves? Don’t know if with elder brother come to study together!!

Introduction to HTTP and TCP

HTTP is an application layer protocol. TCP is a transport layer protocol. HTTP is built on top of TCP. To put it simply, TCP is simply a connection that does not involve any actual data that we need to request. HTTP is used to send and receive data, that is, for practical purposes.

The process of sending and returning data between client and server needs to create something called TCP Connection;

Because TCP does not exist the concept of connection, only exist request and response, request and response are packets, between them are created by TCP from a client initiated, the server received similar connection channel, the connection can always be maintained, HTTP request is sent on the basis of this connection;

Multiple HTTP requests can be sent over a TCP connection, and this pattern varies with different versions.

  • HTTP / 1.0:This TCP connection is created synchronously when the HTTP request is created. When the HTTP request is sent to the server, the server responds, and the TCP connection is closed.
  • HTTP / 1.1:In a way, you can declare that the connection is always alive, and that after one request is transmitted, another request can be transmitted. The advantage of this is that during the creation of a TCP connection, the consumption of “three-way handshake” is required. “three-way handshake” represents three network transfers. If the TCP connection remains, the second request is sent without the consumption of this “three-way handshake.”
  • HTTP/2HTTP requests can also be transmitted concurrently within the same TCP connection.

Introduction to the FORMAT of TCP packets

Enumeration of important fields

Sequence number:

Seq number, which is a 32-bit number, identifies the byte stream to be sent from the TCP source to the TCP destination. This number is marked when the initiator sends data.

Acknowledgement Number:

Ack id, which contains 32 bits. The Ack id field is valid only when the Ack flag bit is 1. Ack=Seq+1.

Flags:

There are six URG, ACK, PSH, RST, SYN, and FIN. The meanings are as follows:

  • URG:The Urgent Pointer is valid.
  • ACK:Verify that the serial number is valid.
  • PSH:The receiver should send the packet to the application layer as soon as possible.
  • RST:Reset the connection.
  • SYN:Initiate a new connection.
  • FIN:Release a connection.

Pay attention to

  • Do not confuse the acknowledgement sequence Ack with the Ack in the flag bit.
  • Ack of the confirming party = Seq+1 of the initiating party.

TCP three-way handshake

The basic condition for establishing a TCP connection is that there is one client and one server.

The application process that initiates the connection is called a client. An application process that passively waits for a connection to be established is called a server.

When client A wants to access the services on server B and establish A connection with server B, the handshake process is as follows:

Popular presentation

For example: compare the client to a boy and the server to a girl. Use their interactions to illustrate the “three handshakes” process:

The boy liked the girl, so he wrote a letter to the girl: I love you, please come with me. ; After writing the letter, the boy waited anxiously because he did not know if the letter would reach the girl smoothly.

After the girl received the boy’s love letter, she was elated. It turned out that we were two lovers. So to the boy wrote a reply: I received your love letter, also understand your mind, in fact, I also like you! I want to date you! ; After writing the letter, the girl also waited anxiously, because she did not know whether the reply could be smoothly conveyed to the boy.

(3) The boy was very happy after receiving the letter, because the girl had received the love letter, and he knew from the letter that the girl liked him and was willing to have a relationship with him. Then the boy wrote a letter to the girl: I have received your letter, thank you, and I love you!

After the girl received the boy’s reply, she was also very happy, because the boy had received the love letter. Both boys and girls know each other’s intentions, and then they communicate happily

Three handshake dynamic diagram demonstration

First handshake

The client sends a connection request packet segment to the server. SYN=1, ACK=0, seq=x in the header of the packet. After the request is SENT, the client enters the SYN-sent state.

  • SYN=1, ACK=0 Indicates that the packet is a connection request packet.
  • X is the initial serial number of the byte stream in the TCP communication.
  • A SYN=1 packet segment cannot have a data part, but consumes a sequence number.

Second handshake

After receiving the connection request packet segment and agreeing to the connection, the server sends a response: SYN=1, ACK=1, SEq = Y, ACK= x+1. After the reply is sent, the syn-RCVD state is entered.

  • The SYN = 1, ACK = 1Indicates that the packet is a reply packet with connection approval.
  • seq=yRepresents the initial serial number of the byte stream to be sent when the server acts as the sender.
  • ack=x+1Indicates that the server expects the next datagram to send bytes numbered from x+1.

Third handshake

After receiving the connection approval reply, the client sends an acknowledgement packet to the server, indicating that the connection approval reply sent by the server is successfully received.

The header of the packet is ACK=1, SEq =x+1, ACK= y+1. The client enters the ESTABLISHED state after sending the packet, and the server enters the ESTABLISHED state after receiving the response.

Why three handshakes

During the first handshake, the SYN from the client arrives at the server after the time has timed out. At this time, although the client knows that it has timed out, the server does not know that it is just sent, and will reply with an ACK to establish a unified connection.

If the three-way handshake is not adopted, the connection is formally established after the server sends an acknowledgement. However, because the client does not send an ACK request to establish a connection, it does not respond to the ACK request or send data to the server. As a result, the server is always in a waiting state, wasting a lot of resources.

When a three-way handshake is used, the server does not request a connection because the server does not acknowledge the SYN sent to the client.

Four times to wave

The so-called quad wave is the release (disconnection) of the TCP connection. The connection must be released actively on one side and passively on the other. The following is a diagram of the client initiating the release of the connection:

Popular interpretation of the

For example: compare the client to a boy and the server to a girl. Illustrate the “four waves” process through their breakup.

  • "First wave" :After a long time of seeing people, the boy found that the girl became his hate appearance, unbearable, so decided to break up, immediately wroteThe first letterTell the girl: Let’s break up.

  • "Second wave" :After the girl received the letter, she knew that the boy wanted to break up with her. She was so angry that she scolded him in the dark: What are you? At the beginning, you were not like this! So he immediately gave it to the boyA letter came back: Break up break up, give me some time, I want to put your things in order, all back to you!

After receiving the girl’s first letter, the boy understood that the girl agreed to break up with him. Then she waited for the girl to put her things away.

  • "Third wave" :After a few days, the girl put all the things the boy sent in order, soTo write againTo the boy: I put your things in order, quickly take them away, from you and ME forever!
  • "Fourth wave" :After the boy received the girl’s second letter, he knew that the girl had packed up her things and could formally break upWriting for the last timeTell the girl: good, bye, I wish you happiness!

“Down to business”

First wave

If A thinks the data is sent, it needs to send A connection release request to B. The request has only a header, and the main parameters in the header are:

The FIN = 1, seq = u. In this case, A enters the FIN-WaIT-1 state.

  • FIN=1Indicates that the message is a connection release request.
  • seq=u, u-1 is the sequence number of the last byte sent from A to B.

Second wave

When B receives the connection release request, it notifies the corresponding application that the connection from A to B has been released. In this case, USER B enters the close-wait state and sends A reply for connection release. The packet header contains ACK=1, SEq = V, ACK= U +1.

  • ACK = 1:Except for the TCP connection request packet segment, the ACK value of all data packets during TCP communication is 1, indicating the reply.
  • seq=v, v-1 is the sequence number of the last byte sent from B to A.
  • ack=u+1Indicates that you want to receive the packet segment starting from the u+1 byte and the first U byte has been received successfully.

After receiving the response, user A enters the FIN-WaIT-2 state and waits for user B to send the connection release request.

After the second wave is completed, the connection from A to B is released, and B will not receive any more data, and A will not send any more data. But the connection from B to A still exists, and B can continue to send data to A.

Third wave

After sending all data to A, B sends A connection release request to A. The request header is FIN=1, ACK=1, SEq = W, ACK= U +1. B enters the last-ACK state.

Fourth wave

After receiving the release request, user A sends A confirmation reply to user B. User A enters the time-wait state. This state will last for 2MSL. If there is no resending request from B within this period, the state will be CLOSED and TCB will be cancelled. After B receives the confirmation reply, it will enter the CLOSED state and cancel TCB.

Why do you wave four times

“1” because the Server needs to send data to the Client before it can be disconnected. If it is waved three times, the connection may be disconnected before the data is sent, resulting in incomplete data. (The Server sends only one ACK signal to the Client for three waves)

“2” The Client may fail to receive the FIN signal from the Server (loss). In this case, the Server is disconnected and the Client waits for the FIN signal. (There is no last ACK signal sent from Client to Server with three waves)

Why TIME_WAIT?

The Client can be disconnected only after the Server is disconnected. The Server may not receive the final ACK signal from the Client. If the Client is disconnected, the Server will wait forever.

The size of TIME_WAIT is generally 2MSL (Maximum Segment Lifetime).

IT brother

A big factory to do advanced Java development program ape

Follow wechat public account: IT elder brother

Java actual combat project video tutorial: you can get 200G, 27 sets of actual combat project video tutorial

Reply: Java learning route, you can get the latest and most complete a learning roadmap

Re: Java ebooks, get 13 must-read books for top programmers

Java foundation, Java Web, JavaEE all tutorials, including Spring Boot, etc

Reply: Resume template, you can get 100 beautiful resumes