TCP is a connection-oriented protocol, and the establishment and release of a connection is an essential process in every connection-oriented communication. The management of the TCP connection is to enable the establishment and release of the connection to proceed normally.
three handshake
Establishment of TCP connection - Three-way handshake to establish a TCP connection
① If 主机A
a client process is running, when it needs the service of host B, it will initiate a TCP connection request, and in the sent segment, it will represent the connection request SYN=1
and generate a TCP connection request 随机发送序号x
. If the connection is successful, A will use x as The initial value of its sending sequence number: seq=x
. Host B completes the first handshake after receiving the connection request message from A.
- The client sends
SYN=1
a connection request - The client sends one
随机发送序号x
. If the connection is successful, A will use x as the initial value of its sending sequence number:seq=x
② 主机B
If you agree to establish a connection, send a confirmation message to host A, use SYN=1
and ACK=1
to indicate that you agree to the connection, and use to ack=x+1
indicate that you have correctly received A's connection request with the serial number x, and also choose one for yourself as 随即发送序号seq=y
the initial value of its sending sequence number. Host A receives the request response message from host B and completes the second handshake .
- The server sends
SYN=1
andACK=1
agrees to the connection - The server sends
ack=x+1
a connection request with sequence number x indicating that A has been received correctly - The server sends an
随即发送序号seq=y
initial value as its send sequence number
③ After host A receives the confirmation from host B, it also needs to send a confirmation to host B to ACK=1
express its agreement to the connection, to ack=y+1
indicate receipt of B's response to the connection, and to send the first data of A at the same time seq=x+1
. Host B receives the acknowledgment message from host A and completes the third handshake . At this time, the two parties can use the agreed parameters and the resources allocated by each to carry out normal data communication.
- The client sends
ACK=1
an agreement to connect - The client sends
ack=y+1
to indicate that it has received B's response to the connection, and at the same time sends the first data of Aseq=x+1
Why do we need a three-way handshake, but not two?
In order to confirm that the receiving and sending capabilities of both parties are normal.
Through the third handshake, the main purpose is 为了防止已失效的连接请求报文段突然又传送到了主机B,因而产生错误
.
The so-called "invalid connection request segment" refers to the connection request sent by one end (such as A), because it is not transmitted to the destination (such as B) within the allowed time, so that the sender has to send a new connection request segment.
However, after a new connection request is established and the data is transferred and the connection is released, a situation occurs that the first connection request segment sent by host A arrives at B late. Originally, this was an invalid segment, but after receiving the invalid connection request segment, host B mistakenly believed that host A sent a new connection request, so it sent a confirmation segment to host A , agree to establish a connection. Since host A does not request to establish a connection, it will ignore the confirmation of host B and will not send data to host B. But host B thinks that the transmission connection has been established in this way, and has been waiting for host A to send data. Many resources of host B are wasted in this way.
Using the three-way handshake mechanism can prevent the occurrence of the above phenomenon. In the above case, host A will ignore the confirmation sent by host B, and will not send a confirmation message to host B, and the connection will not be established.
waved four times
Dismantling of the TCP connection - release the TCP connection with a four-way handshake
① After the data transmission is over, both communication parties can release the connection. In the figure above, the application process of host A first sends a connection release request to its TCP, and no longer sends data. TCP notifies the other party to release the connection from A to B, and then sends FIN=1
a segment to host B, which 序号u
is equal to the sequence number of the last byte of the transmitted data plus 1. At this time, A is in the state of waiting for B to confirm.
② After the TCP of host B receives the notification of releasing the connection, it sends out an acknowledgment, 确认序号ack=u+1
and this message segment itself 序号是v
is equal to the serial number of the last byte of the data that host B has transmitted plus 1, and at the same time notifies the high-level application process. In this way, the connection from A to B is released, and the connection is in a half-closed state, that is, host A has no data to send, but if host B sends data, A still needs to receive it. In other words, the connection from B to A has not been closed, and it may take a while. The reason for waiting is that if host B still has some data to send to host A, it can continue to send. As long as host A receives the data, it should still send an acknowledgment to host B.
③ After the data transmission from host B to host A ends, its application process notifies TCP to release the connection. The connection release message segment sent by host B must FIN置1
first assume B's 序号为w
(host B may have sent some data in the half-closed state), and must also repeat what has been sent last time 确认序号ack=u+1
. At this time, host B enters the state of waiting for A's confirmation.
ACK置1
④ After host A receives B's connection release message segment, it must issue 确认序号ack=w+1
an acknowledgment 序号是seq=u+1
. The connection from B to A is released. The TCP of host A reports to its application process that the entire connection has been released.
Why do you need to wave your hand four times?
The reason why TCP uses four waves is because the TCP connection is full-duplex, so the two parties need to release the connection to the other party separately. The release of the connection on one side only means that data can no longer be sent to the other party, and the connection is half-released. state.
The reason why the client will wait for a period of time before closing after the last wave is to prevent the confirmation segment sent to the server from being lost or wrong, which will cause the server to fail to close normally.