1. TCP/IP protocol suite TCP/IP is a protocol family, usually developed in different layers, each layer is responsible for different communication functions. It includes the following four layers: Link layer: Also known as the data link layer or network interface layer, it usually includes the device driver in the operating system and the corresponding network interface card in the computer. Together, they handle the details of the physical interface with the cable (or any other transmission medium). Network layer: Also known as the Internet layer, it handles the activities of packets in the network, such as packet routing. Network layer protocols include IP (Internet Protocol), ICMP (Internet Control Message Protocol), and IGMP (Internet Group Management Protocol). The transport layer mainly provides end-to-end communication between applications on two hosts: There are two different transport protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). TCP provides highly reliable data communication between two hosts. Its work includes breaking the data given to it by the application into appropriate small pieces and handing them to the network layer below, confirming the received packets, setting the timeout clock for sending the last confirmation packet, etc. Since the transport layer provides highly reliable end-to-end communication, the application layer can ignore all these details. On the other hand, UDP provides a very simple service to the application layer. It just sends packets called datagrams from one host to another, but it does not guarantee that the datagram will reach the other end. Any necessary reliability must be provided by the application layer. The application layer is responsible for handling application-specific details: Including Telnet (remote login), FTP (File Transfer Protocol), SMTP (Simple Mail Transfer Protocol) and SNMP (Simple Network Management Protocol). 2. Introduction to TCP protocol TCP is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. TCP packages user data into segments, starts a timer after sending, confirms the data received at the other end, reorders out-of-order data, and discards duplicate data. The characteristics of TCP are:
The TCP message header is shown in the following figure:
3. Detailed explanation of the three-way handshake process The so-called three-way handshake is to establish a TCP connection, which means that when establishing a TCP connection, the client and the server need to send a total of 3 packets to confirm the establishment of the connection. In socket programming, this process is triggered by the client executing connect. The whole process is shown in the figure below: (1) First handshake: The client sets the SYN flag to 1, randomly generates a value seq=J, and sends the data packet to the server. The client enters the SYN_SENT state and waits for confirmation from the server. (2) Second handshake: After receiving the data packet, the server knows from the flag bit SYN=1 that the client requests to establish a connection. The server sets the flag bits SYN and ACK to 1, ack=J+1, randomly generates a value seq=K, and sends the data packet to the client to confirm the connection request. The server enters the SYN_RCVD state. (3) The third handshake: After receiving the confirmation, the Client checks whether ack is J+1 and ACK is 1. If correct, the flag ACK is set to 1, ack=K+1, and the data packet is sent to the Server. The Server checks whether ack is K+1 and ACK is 1. If correct, the connection is successfully established, the Client and Server enter the ESTABLISHED state, and the three-way handshake is completed. Then the Client and Server can start transmitting data. 4. Detailed explanation of the four-wave process The so-called Four-Way Wavehand, which terminates a TCP connection, means that when a TCP connection is disconnected, the client and the server need to send a total of 4 packets to confirm the disconnection. In socket programming, this process is triggered by either the client or the server executing close. The whole process is shown in the figure below: Since TCP connection is full-duplex, each direction must be closed separately. The principle is that when one party completes the data sending task, it sends a FIN to terminate the connection in this direction. Receiving a FIN only means that there is no data flow in this direction, that is, no more data will be received, but data can still be sent on this TCP connection until FIN is sent in this direction. The party that closes first will perform an active close, while the other party will perform a passive close, as described in the figure above. 1st wave: The client sends a FIN to close the data transmission from the client to the server, and the client enters the FIN_WAIT_1 state. Second wave: After receiving FIN, the server sends an ACK to the client, confirming that the sequence number is the received sequence number + 1 (the same as SYN, one FIN occupies one sequence number), and the server enters the CLOSE_WAIT state. The third wave: The server sends a FIN to close the data transmission from the server to the client, and the server enters the LAST_ACK state. The fourth wave: After the client receives the FIN, it enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the sequence number is the received sequence number + 1. The server enters the CLOSED state, completing four waves. Conclusion Why is it that establishing a connection requires three handshakes, but closing the connection requires four handshakes? This is because when the server is in the LISTEN state, after receiving the SYN message of the connection establishment request, it puts the ACK and SYN in one message and sends it to the client. When closing the connection, when receiving the other party's FIN message, it only means that the other party will no longer send data but can still receive data. The party may not have sent all the data to the other party, so the party can close immediately, or send some data to the other party, and then send a FIN message to the other party to indicate that it agrees to close the connection now. Therefore, the ACK and FIN of the party are generally sent separately. |
<<: How to use flex elegantly on mobile devices in 2017
>>: Summary of common tools and third-party libraries for Android development
Mid-Autumn Festival, the night of family reunion,...
According to foreign media reports, as of now, al...
gossip Recently, all major platforms have been pu...
It is no exaggeration to say that I am a typical ...
A classmate asked me about the gameplay of "...
Sudden cardiac death (SCD) remains the leading ca...
On every day when people can legitimately grab re...
According to the year-end forecast of mobile data...
Japanese cars are important participants in the g...
As a leader in new energy vehicles, BYD has taken...
This article was reviewed by Pa Li Ze, chief phys...
Eiichiro Misaki, the author of "Don't Tel...
How to apply for a 400 service phone number? How ...
All along Kunming They are all well-known "S...
Whether it is an old brand that has quickly becom...