This is a creation in Article, where the information may have evolved or changed.
I. Characteristics of TCP
TCP provides a connection-oriented, reliable byte-stream service
In a TCP connection, only two parties communicate with each other. Broadcast and multicast cannot be used with TCP
TCP uses checksums, acknowledgements, and retransmission mechanisms to ensure reliable transmission
TCP Sorts the data sections and uses the cumulative acknowledgement to ensure that the data is in the same order and is not duplicated
TCP uses the sliding window mechanism to achieve traffic control by dynamically changing the size of the window for congestion control
Note: TCP does not guarantee that the data will be received by the other party because this is not possible. What TCP can do is to deliver the data to the receiver if it is possible, otherwise it notifies the user (by discarding retransmission and interrupting the connection). Therefore, it is accurate to say that TCP is not a 100% reliable protocol, it can provide reliable delivery of data or failure of reliable notification.
Two. Three handshakes and four waves
The so-called three-time handshake (three-way handshake) means that when a TCP connection is established, the client and server are required to send a total of 3 packets.
The purpose of the three-time handshake is to connect the server to the specified port, establish a TCP connection, and synchronize the serial number and confirmation number of both parties, exchanging TCP window size information. In socket programming, the client executes connect (). Will trigger a three-time handshake.
First Handshake (Syn=1, Seq=x):
The client sends a TCP SYN flag to the 1 packet, indicating the port of the server to which the client intends to connect, as well as the initial ordinal X, which is saved in the header Number field of the Sequence. After sending, the client enters the Syn_send state.
Second Handshake (Syn=1, Ack=1, Seq=y, acknum=x+1):
The server sends back a confirmation packet (ACK) reply. That is, the SYN flag bit and the ACK flag bit are all 1. Server-side choose their own isn serial number, put in the Seq domain, while the confirmation number (acknowledgement numbers) is set to the customer's ISN plus 1, that is, x+1. After sending, the server side enters the SYN_RCVD state.
Third Handshake (ack=1,acknum=y+1)
The client sends the confirmation packet (ACK) again, the SYN flag bit is the 0,ACK flag bit is 1, and the server sends an ACK to the ordinal field +1, which is sent to the other party in the determination field, and the client enters the established state after the +1 of the data segment is written. When the server receives this packet, it also enters the established state, and the TCP handshake ends.
The removal of TCP connections requires the sending of four packets, so called four waves (Four-way handshake), also known as the improved three-time handshake. Either the client or the server can initiate a wave gesture, and in socket programming, any party performs a close () operation to generate a wave.
Wave for the first time (FIN=1,SEQ=X)
Assuming that the client wants to close the connection, the client sends a package with a FIN flag position of 1, indicating that no data can be sent, but can still accept the data. After sending, the client enters the fin_wait_1 state.
Second Wave (ack=1,acknum=x+1)
The server-side confirms the client's FIN package, sends a confirmation packet, indicates that it has accepted the client's request to close the connection, but is not ready to close the connection. After sending, the server side enters the close_wait state, the client receives this confirmation packet, enters the fin_wait_2 state, waits for the server side to close the connection.
Wave for the third time (fin=1,seq=y)
When the server is ready to close the connection, send the end connection request to the client, and the FIN is set to 1.
After sending, the server side enters the Last_ack state, waiting for the last ACK from the client.
The
Fourth wave (ack=1,acknum=y+1)
Client receives a shutdown request from the server, sends a confirmation packet, and enters the time_wait state, waiting for possible ACK packets that require retransmission. After the server receives this acknowledgement package, it closes the connection and enters the CLOSED state. The client waits for a fixed time (two maximum segment life cycle, 2msl,2 Maximum Segment Lifetime), does not receive the server side of the ACK, the server side has been properly shut down the connection, so he also closed the connection, into the CLOSED state.