TCP (Transmission Control Protocol) works by establishing a reliable connection between two devices, ensuring data is delivered in the correct order and without errors through a multi-step process. The key steps are connection establishment, data transfer, and connection termination.
1. Connection Establishment (Three-Way Handshake)
The three-way handshake initiates a TCP connection:
-
SYN (Synchronize): The client (e.g., your web browser) sends a SYN packet to the server, indicating it wants to establish a connection. This packet contains a random initial sequence number (ISN) - let's call it
x
- that will be used to track the data flow. -
SYN-ACK (Synchronize-Acknowledge): The server receives the SYN packet and responds with a SYN-ACK packet. This acknowledges the client's SYN (ACK number is
x+1
) and also sends its own SYN with its own initial sequence number (let's call ity
). -
ACK (Acknowledge): The client receives the SYN-ACK packet from the server. It then sends an ACK packet back to the server, acknowledging the server's SYN (ACK number is
y+1
). At this point, the TCP connection is established, and data transfer can begin.
Packet | Source | Destination | Flags | Sequence Number | Acknowledgment Number |
---|---|---|---|---|---|
SYN | Client | Server | SYN | x | - |
SYN-ACK | Server | Client | SYN, ACK | y | x+1 |
ACK | Client | Server | ACK | x+1 | y+1 |
2. Data Transfer (Sending and Receiving Segments)
Once the connection is established, data is transmitted in segments:
-
Segmentation: The sending application breaks down the data into smaller segments. Each segment includes a TCP header with sequence numbers, acknowledgment numbers, and other control flags.
-
Sequence Numbers: Each segment is assigned a sequence number, allowing the receiver to reassemble the data in the correct order if segments arrive out of order.
-
Acknowledgment: The receiver sends acknowledgment (ACK) packets to the sender to confirm that it has received the data segments. The ACK number indicates the next sequence number the receiver expects to receive.
-
Retransmission: If the sender does not receive an ACK within a certain timeout period, it retransmits the unacknowledged segment, ensuring reliable delivery. TCP uses mechanisms like selective acknowledgment (SACK) to efficiently retransmit only the missing segments.
-
Flow Control: TCP implements flow control mechanisms (using a "window size") to prevent the sender from overwhelming the receiver with data. The receiver advertises the amount of data it can accept.
-
Congestion Control: TCP employs congestion control algorithms (like TCP Reno, TCP Cubic) to dynamically adjust the sending rate based on network congestion. This prevents network overload and ensures fair bandwidth sharing.
3. Connection Termination (Four-Way Handshake)
When the communication is complete, the connection is terminated using a four-way handshake:
-
FIN (Finish): One of the parties (either the client or server) sends a FIN packet to signal that it has no more data to send.
-
ACK (Acknowledge): The other party receives the FIN packet and sends an ACK packet to acknowledge it. This signifies that it has received the request to close.
-
FIN (Finish): The other party then sends its own FIN packet to signal that it is also ready to close the connection.
-
ACK (Acknowledge): The original sender receives the FIN packet and sends a final ACK packet. The connection is now closed.
Packet | Source | Destination | Flags | Sequence Number | Acknowledgment Number |
---|---|---|---|---|---|
FIN | Client | Server | FIN | z | - |
ACK | Server | Client | ACK | - | z+1 |
FIN | Server | Client | FIN | w | - |
ACK | Client | Server | ACK | - | w+1 |
In summary, TCP provides reliable, ordered, and error-checked delivery of data over an IP network using connection establishment, data transfer with acknowledgments and retransmissions, flow control, congestion control, and connection termination.