TCP connection three-way handshake and four waves

establish connection

The client and server send requests through the HTTP protocol and obtain content.

Before sending a request, you need to establish a connection first and make sure that the target machine is in a state that can accept the request.

For example, if you want to ask a courier (a third-party) to pick up something from Zhang San's house, you must first call him to ask if he is at home. This is the process of establishing a connection.

The HTTP protocol is an application layer protocol. It only specifies the data format of req and res, such as status code, header, body, etc.

Establishing a network connection requires a lower-level TCP protocol.

three handshakes

Three-way handshake, that is, when establishing a TCP connection, the client and server need to send a total of 3 packets.

Let me give you an example first. Or do you want to send someone to Zhang San’s house to pick up something? Now you have to send a text message (not call) to “establish a connection”, which requires at least 3 steps, none of which can be eliminated.

  • Are you at home?
  • Zhang San: At home
  • You: Okay, let’s go there (then you assign someone to come to the door, and Zhang San is ready to greet him)

process

  • The client sends the packet and the server receives it. Server confirmation: The client's sending capability is normal.
  • The server sends the packet and the client receives it. Client confirmation: The receiving capability of the server is normal.
  • The client sends the packet and the server receives it. Server confirmation: The client is about to send me data, and I want to be ready to receive it.

The connection is established, and then data and communication begin.

wave four times

Handshake is to establish a connection. Waving means saying goodbye and closing the connection.

Still the same example as before. Picking things up may not be done in one go, it may take many trips back and forth. Moreover, it may not all be initiated by you. During the process, Zhang San may also take the initiative to send someone to send it to you.

That is, what you see in Chrome is an http request. In fact, it may require several network transmissions, but the browser merges them together.

Okay, after picking up the things, you need to send a text message "Close the connection" to tell Zhang San that it is time to close the door. It takes 4 steps.

[Note] Here you need to wait for confirmation that Zhang San closes the door before the connection is completely closed. You cannot just say it and ignore it. It’s different from everyday life.

  • You: It’s done
  • Zhang San: Okay (I may continue to send you messages at this time, and you have to continue to receive them. Until Zhang San has finished sending)
  • Zhang San: I have finished sending and am ready to close the door.
  • You: OK, close the door (then you can leave, Zhang San can close the door, the connection is ended)

process

  • The client sends the packet and the server receives it. Server confirmation: The client has completed the request
  • The server sends the packet and the client receives it. Client confirmation: Server has received it, I'm waiting for it to close
  • The server sends the contract: the client accepts it. Client confirmation: The server has completed sending and can be closed
  • The client sends the packet and the server receives it. Server confirmation: can be closed

Illustration

Insert image description here

Guess you like

Origin blog.csdn.net/wuguidian1114/article/details/125964154