I finally get to know the TCP three-way handshake and four waving (Photo Case Detailed)

TCP three-way handshake and four waving either in development or interview is a very important knowledge, it is the basis for us to optimize web application performance. Welcome to learn and progress together

About a .TCP

  • TCP (Transmission Control Protocol, Transmission Control Protocol) 是一种面向连接的、可靠的、基于字节流的通信协议, data before transmission to establish a connection, but also disconnected after the transfer is completed.
  • Before the client to send and receive data using the connect () function and the connections to the server. Purpose is to ensure the establishment of the connection IP address, port, and other physical link is correct, open channel for the transmission of data.
  • TCP to transmit three packets a connection is established, commonly known as three-way handshake (Three-way Handshaking)

Here Insert Picture Description

Two .TCP Datagram structure

Here Insert Picture Description

  • ① number : Seq (Sequence Number) accounted for 32-bit serial number used to identify the sequence number sent from computer A to computer B data packets, which are labeled when the computer sends data.
  • ② acknowledgment number : Ack (Acknowledge Number) accounted for 32-bit acknowledgment number, the client and server can send, Ack = Seq + 1.
  • ③ flag : each flag occupies 1Bit, a total of six, respectively, URG, ACK, PSH, RST, SYN, FIN, the following specific meanings:

URG: urgent pointer (urgent pointer) is valid.
ACK: acknowledgment number is valid.
PSH: receiver should be the message to the application layer as soon as possible.
RST: Reset the connection.
SYN: to establish a new connection.
FIN: disconnecting a connection.

Three .TCP three-way handshake

  • 过程描述
  • ① First Client sends a connection request packet,
  • ②Server period after receiving the connection reply ACK packet, and allocate resources for this connection.
  • ③Client ACK packet also occur after receiving ACK packet to the Server segment and allocate resources so that the TCP connection is established.

Here Insert Picture Description

  • For example child with Sichuan Airlines
  • ① Sichuan 8633 to establish a connection request (SYN), and sends the sequence number.
  • ② service termination by the signal, i.e. confirmation number (the ACK), and at this time the same sequence number Seq return request
  • ③ client receives signals, i.e., confirmation number (the ACK), connection has been established.
    Here Insert Picture Description
  • Summary : 三次握手的关键是要确认对方收到了自己的数据包This goal is achieved by "acknowledgment number (Ack)" field. The computer will record packet sequence number Seq own transmitted until the other party receives the packet, detects "acknowledgment number (the Ack)" field 看Ack = Seq + 1是否成立,如果成立说明对方正确收到了自己的数据包.

  • If only two handshakes. 这个时候客户端没有回应,这样会浪费服务端的资源
    Here Insert Picture Description

That if you thought about why you need a third communication?

  • In the first communication process, after the transmission information A to B, B after receiving the acknowledgment information reception capability and their ability to transmit A no problem.
  • In the second communication, send a message to B after A, A can confirm their ability B signaling capability and receiving no problem, but do not know how their B signaling capability in the end, so we need a third communication .
  • In the third communication, after the transmission information A to B, B can confirm their ability to transmit no problem.
  • Summary : 3-way handshake to complete two important functions 既要双方做好发送数据的准备工作(双方都知道彼此已准备好), but also to allow the parties to negotiate on the initial sequence number, the serial number is sent and acknowledged during the handshake process.

Four .TCP of four wave

  • To establish a connection is very important, it is a prerequisite for correct data transmission; disconnected Equally important, it allows the computer to release the resources no longer in use. If the connection is not normally open, not only result in data transmission errors, but also lead to the socket can not be closed, continued to consume resources, if the amount of high concurrency, server stress worrying.

// Process Description

  • A: "task processing is completed, and I want to disconnect."
  • B: "? Oh, yeah. Please hold while I get ready."
  • After waiting for a while ......
  • B: "I'm ready to be a disconnect."
  • A: "Yes, thank you."

Here Insert Picture Description

  • Sichuan Airlines diagram example
  • ① application server is disconnected i.e. FIN, Seq + Ack transmission
  • ② client receives information back, I have received indicate
  • The client sends information indicating ③ can be disconnected
  • ④ server to accept the information and return the data has been accepted information

Here Insert Picture Description

  • After the data transfer is completed, the two sides can release the connection. The very beginning, the client and server are in ESTABLISHED state, and then take the initiative to shut down the client, server, passive close.

Why when the connection is three-way handshake, when it is closed handshake?

  • ① Because when SYN Server Client terminal end receives connection request message may be sent directly SYN + ACK packet. Wherein the ACK message is used for response, SYN etc is used to synchronize the packet.
  • ② However, when the connection is closed, when the Server side FIN packet is received, probably does not close immediately SOCKET, we can only respond to a first ACK packet, told Client-side, "you send FIN messages I have received." .
  • ③ only until the end of all my Server messages are sent over, I can send FIN packets, and therefore can not be sent together. It requires a four-step handshake.

  • 补充问题
  • TCP three-way handshake will be able to guarantee reliable transmission do?不能
  • Three-way handshake is more reliable than twice, but it is not reliable, and add more handshake not make the connection more reliable. So choose the three-way handshake.
  • Completely reliable communication protocols do not exist in the world. From the communication time cost and space cost in terms of reliability, select the general rule "three-way handshake" as a point to point communication.

  • The best investment is in yourself.

Here Insert Picture Description

  • 2020.03.19 来自辰兮的第32篇博客
Published 35 original articles · won praise 116 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_45393094/article/details/104965561