TCP Protocol Introduction

The Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream oriented transport layer protocol used for reliable data transmission over computer networks.

The TCP protocol provides the following characteristics:

  1. Reliability: TCP ensures the reliability of data transmission by using sequence numbers, acknowledgments, and retransmission mechanisms. If the receiver fails to correctly receive a packet, the sender will retransmit the lost packets until the receiver confirms correct receipt of the data.
  2. Connection-Oriented: Before transmitting data, TCP requires establishing a connection. Data transmission can only occur after the connection is established. Upon completion of transmission, the connection must be explicitly closed.
  3. Flow Control: TCP controls the sender's transmission rate using the sliding window mechanism to prevent the receiver from being overwhelmed by excessive packets.
  4. Congestion Control: TCP regulates the data transmission rate using the network congestion window to avoid network congestion.
  5. Reliable Data Transmission: TCP guarantees the order and integrity of data. The order in which packets arrive at the receiver matches the order in which they were sent by the sender, ensuring that the receiver obtains a complete copy of the data sent by the sender.
  6. Full-Duplex Communication: TCP allows both the sender and receiver to transmit and receive data simultaneously. This makes data transmission more efficient and flexible.

In summary, TCP is a reliable, connection-oriented, full-duplex protocol suitable for most applications requiring reliable data transmission.

TCP Lifecycle

The TCP lifecycle refers to the entire process of establishing and closing a TCP connection, which includes the following stages:

  1. Connection Establishment: The lifecycle of a TCP connection begins with its establishment. During this process, the client and server negotiate initial sequence numbers through a three-way handshake, confirm connection parameters, and establish a reliable data transmission channel.
  2. Data Transmission: After the connection is established, the client and server can exchange data. During data transmission, the TCP protocol provides reliable data transmission mechanisms to ensure data integrity and correctness.
  3. Connection Termination: When no further data transmission is needed between the client and server, the connection must be closed. The termination process requires a four-way handshake to ensure proper closure of the connection.
  4. Waiting: During the TCP connection lifecycle, waiting states may occur, such as waiting for a response from the peer or waiting for network congestion to subside. In these waiting states, the TCP protocol selects appropriate waiting times based on specific conditions and manages the connection accordingly.
  5. Timeout: Timeout situations may arise during the TCP connection lifecycle, such as wait timeouts or retransmission timeouts. When a timeout occurs, the TCP protocol handles the situation appropriately, such as retransmitting packets or closing the connection.
  6. Error Handling: Various errors may occur during the TCP connection lifecycle, such as network failures, packet loss, or packet corruption. Upon encountering an error, TCP performs corresponding error handling based on the specific situation, such as retransmitting packets, closing the connection, or re-establishing the connection.

In summary, the TCP connection lifecycle includes processes such as connection establishment, data transmission, connection termination, waiting, timeout, and error handling. The TCP protocol manages and processes these situations appropriately to ensure connection reliability and correctness.

Three-Way Handshake

The TCP three-way handshake is the process of establishing a TCP connection and consists of the following three steps:

  1. First Handshake: The client sends a SYN (synchronize) packet to the server, specifying its initial sequence number (ISN).
  2. Second Handshake: Upon receiving the client's SYN packet, the server sends a SYN+ACK (synchronize + acknowledge) packet, acknowledging the client's SYN and specifying its own initial sequence number (ISN).
  3. Third Handshake: Upon receiving the server's SYN+ACK packet, the client sends an ACK (acknowledge) packet, acknowledging the server's SYN+ACK and specifying the next sequence number.

During the three-way handshake process, both the client and the server must send SYN and ACK packets to establish a reliable connection. The purpose of the three-way handshake is to ensure that both the client and the server can receive each other's packets and correctly identify and acknowledge each other's initial sequence numbers.

After completing the three-way handshake, the TCP connection is established, and data can be transmitted over the connection. When the connection is no longer needed, a four-way handshake can be used to close it.

Four-Way Handshake

The TCP four-way handshake is the process of closing a TCP connection and consists of the following four steps:

  1. First Handshake: The client sends a FIN (finish) packet to the server, indicating that the client has completed data transmission.
  2. Second Handshake: Upon receiving the client's FIN packet, the server sends an ACK (acknowledge) packet, indicating that it has received the client's FIN packet.
  3. Third Handshake: The server sends a FIN packet to the client, indicating that it has completed its data transmission.
  4. Fourth Handshake: Upon receiving the server's FIN packet, the client sends an ACK packet, indicating that it has received the server's FIN packet.

During the four-way handshake process, both the client and the server must send FIN and ACK packets to close the connection and confirm that the other party has received the connection closure request. The purpose of the four-way handshake is to ensure that both the client and the server correctly close the connection and release associated resources.

It should be noted that during the four-way handshake process, both the client and the server can continue to send data after sending a FIN packet until the other end confirms the connection closure. This is because the FIN packet only indicates that the sender has completed its data transmission; it does not imply that the receiver has completed receiving and processing the data.