Summary of TCP normal connection establishment and termination

TCP Summary
         TCP/IP (Transmission Control Protocol/Internet Protocol) is a network communication protocol that regulates all communication devices on the network, especially the data exchange format and transmission method between one host and another.

The TCP header structure has 6 identification bits
URG: Whether the emergency pointer is valid
ACK: Indicates whether the confirmation number is valid (the confirmation flag for the TCP request)
PSH: Indicates that the receiving end application should immediately read the data from the receiving buffer, in order to receive Subsequent data makes room.
RST: Ask the other party to re-establish the connection.
SYN: Request to establish a connection (only valid when the connection is established by the three-way handshake, indicating a synchronization segment)
FIN: The connection with the other end should be closed. (The flag is used to end a TCP connection, indicating that this segment is an end segment) The

three-way handshake establishes a connection

and begins to introduce the process of the three-way handshake:


    The first handshake: In fact, the client sends a datagram with SYN The text segment is sent to the server, which corresponds to the host A->host B in the picture (SYN=1, seq=i), and seq is the corresponding serial number i. After that, host A enters the SYN_SENT state. At the same time, wait for the confirmation of the server. The
    second handshake: After the server receives the synchronization segment (that is, the synchronization segment with SYN), it must determine the client's synchronization segment. Then I also send a segment with SYN, which corresponds to the second segment in the picture, where SYN=1, indicating that this is a synchronous segment, ACK=1, indicating that this is an acknowledgment For the segment, confirm that the sequence number is i+1 and the sequence number is j.
    The third handshake: This time, after the client receives the acknowledgment synchronization segment, it sends an acknowledgment packet to the server, which corresponds to the third segment in the picture. ack=1 means that this is an acknowledgment packet. After confirming that the serial number is j+1

and the three-way handshake is established, the client and the server begin to transmit data.
Three-way handshake diagram:


four-way handshake to close the connection
Next, let's introduce the next four-way handshake to close

    the first handshake: it means that client A sends an end segment with SYN to the server, which is used to close client A to the service The data transmission of end B corresponds to the first segment in the picture, FIN=1, seq=i, which indicates the
    second handshake of an end segment: after server B receives this end segment, It returns a confirmation segment, which corresponds to the second confirmation segment in the picture. Among them, ACK=1, ack=i+i, indicating an acknowledgement segment, and the acknowledgement sequence number is i+1.
    The third handshake: Server B closes the connection with client A, and he sends a segment with SYN ending , corresponding to the third segment on the picture, where SYN=1, seq=j, indicating the end of the segment, and the sequence number is j.
    The fourth handshake: Client A returns an acknowledgement segment, where the acknowledgement sequence number is j+1, which corresponds to the fourth segment on the picture, ACK=1, ack=j+1.
Four-way handshake diagram:



Summary: Why is a three-way handshake required to establish a connection? Why does closing a connection require a four-way handshake? Why is a three-way handshake required to establish a connection?
          TCP connection establishment requires 3 handshakes, and disconnection requires
4 When closing, each direction must be closed separately. This one-way closing is called half closing.
The method of closing is that after one party completes its data transmission, it sends a FIN to notify the other party that the connection in this direction will be terminated. When one end receives a FIN, it must notify the application layer that the TCP connection has terminated data transfer in this direction. Sending a FIN is usually the result of a shutdown by the application layer.

    When closing the connection, when you receive the end segment of the other party, it means that the other party has no data to send to you, but not all your own data has been sent, so you may need to continue sending some data and then send the end segment. Giving it to the other party means that you agree that the connection can be disconnected now, so closing the connection requires four handshakes

    . If you change the three-way handshake to two-way handshake, a deadlock may occur.

The following figure is the state corresponding to the establishment and termination of a normal TCP connection:

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043009&siteId=291194637