TCP/IP protocol three-way handshake and four-way handshake (establish connection and disconnect), tcpip
1. TCP/IP protocol Overview
The TCP/IP Protocol (TransmissionControl Protocol/Internet Protocol) is also called the transmission control/Internet Protocol and network communication Protocol. This Protocol is the basis of the Internet. TCP/IP is the basic communication protocol used in the network. Although TCP/IP includes two protocols, Transport Control Protocol (TCP) and Internet Protocol (IP), TCP/IP is actually a group of protocols, it includes hundreds of various functional protocols, such as remote logon, file transmission, and email. TCP and IP protocols are two basic important protocols to ensure complete data transmission. Generally, TCP/IP is an Internet Protocol family, not just a TCP/IP or an IP. The TCP/IP protocol is widely used. It is the only protocol system used for communication between different network types. It is suitable for connecting multiple models and can be used for both LAN and WAN, many manufacturers' computer and network operating system products use or contain TCP/IP protocols. TCP/IP has become a de facto international standard and industrial standard.
TCP/IP is composed of many different protocols. The TCP user data report Protocol, also known as the TCP transmission Control Protocol (Transport Control Protocol, a reliable host-to-host layer Protocol, the transport control protocol is the fourth layer of the OSI network, and the TCP transmission control protocol is one of the six basic protocols for TCP/IP transmission. ). TCP is a reliable connection-oriented transmission service. It performs data transmission in segments, and the host must establish a session to exchange data. It uses bitstream communication, that is, data is used as a non-structured byte stream. Specify the sequence number for each TCP transmission field to obtain the reliability. YesLayer-4 of the OSI reference model provides reliable data transmission by using the IP address interconnection function. The IP address keeps putting packets on the network, while TCP is responsible for ensuring that packets arrive. TCP is responsible for handshaking process, packet management, traffic control, error detection and processing (control) in the actions of the IP addresses ), messages in an abnormal order can be sorted in a new order according to the order of numbers.
2. Three-way handshake Principle Analysis
Figure 1 three-way handshake
In the early stage of a TCP session, there was a so-called "three handshakes", that is, how to track and negotiate the data volume sent each time to synchronize the sending and receiving of data segments, determines the number of data confirmations based on the amount of data received and when to cancel the contact after the data is sent and received, and establishes a virtual connection. To provide reliable transmission, TCP sends the serial number of the data packet in a specific order before sending new data, and needs the packets to be sent to the confirmation message after the target machine. TCP is always used to send large volumes of data. TCP is also used when the application needs to confirm the received data. Because TCP requires time tracking, this requires additional overhead, making the TCP format somewhat complex.
In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection..
First handshake: When a connection is established, the client sends the syn Packet (syn = j) to the server and enters the SYN_SEND status. wait for confirmation from the server;
Second handshake: When the server receives the syn packet, it must confirm the customer's SYN (ack = j + 1) and send a SYN Packet (syn = k), that is, the SYN + ACK packet, the server enters the SYN_RECV status;
The third handshake:The client receives the server's SYN + ACK packet and sends the ACK (ack = k + 1) Confirmation packet to the server. After the packet is sent, the client and server enter the ESTABLISHED status and complete three handshakes. After the three-way handshake is completed, the client and the server start to transmit data. In the above process, there are some important concepts: unconnected Queues: in the three-way handshake protocol, the server maintains an unconnected queue, this queue opens an entry for the SYN Packet (syn = j) of each client. This entry indicates that the server has received the SYN Packet and sent a confirmation to the customer, waiting for the customer's confirmation package. The connection identified by these entries is in the Syn_RECV state on the server. When the server receives the customer's confirmation packet, it deletes the entry and the server enters the ESTABLISHED state. Backlog parameter: Maximum number of unconnected queues.
SYN-ACK: retransmission count. The server sends the SYN-ACK package. If the customer's confirmation package is not received, the server re-transmits the package for the first time, if the number of retransmission times exceeds the maximum number of retransmission times specified by the system, the system deletes the connection information from the semi-connection queue. Note that the waiting time for each retransmission is not necessarily the same.
Semi-connection survival time: the maximum time for the semi-connection queue to survive, that is, the maximum time for the service from receiving the SYN packet to confirming that the message is invalid, the maximum waiting time of all retransmission request packets. The semi-join survival time is also called Timeout time and SYN_RECV survival time.
In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection. 1.
Figure 2 Four Waves
TCP also needs to confirm each other before the connection can be disconnected, and a connection is closed with four waves, as shown in figure 2. In the first interaction, a request with FIN = 1 is sent, which requires disconnection. The target host sends ACK = 1 for confirmation after receiving the request. After the confirmation information is sent, a fin = 1 packet is sent, Which is disconnected from the source host. Then, the source host returns an ACK = 1 message, so that a complete TCP session ends.
Summary:
The transport layer is the core of the entire protocol layer, while TCP is a connection-oriented transmission protocol used to provide reliable, end-to-end byte stream communication protocols on the unreliable Internet. So. TCP plays an important role in the transmission layer.
In my opinion, establishing a connection during TCP transmission is the most important. The difference between TCP and UDP is also reflected here. TCP needs to establish a connection before data transmission. This ensures the reliability of data transmission.
I still remember that due to the distance between the two armies, in the synchronous combat scheme, a connection should be established to reach an agreement, however, the connection will be established endlessly in the dual transmission because the recipient cannot determine whether to receive the message. The three-way handshake not only maintains reliable communication between the two parties, but also does not make the two sides establish communication endlessly. A timer is added for each message to design a maximum latency, so that the timeout data is regarded as lost and other tasks are not delayed due to endless waiting.