TCP (Transmission Control Protocol)

Software developer writing to think clearly about code, systems, and real-world tradeoffs.
What happens if data is sent without rules?
When data is sent over the internet without any rules:
Data may not reach the destination
Data may arrive in the wrong order
Some data may be lost
Same data may arrive more than once
Sender does not know if data was received or not
Because of these problems, communication becomes unreliable.
What is TCP and why is it needed?
TCP (Transmission Control Protocol) is a communication protocol that makes data transfer reliable.
TCP is needed because it:
Ensures data is delivered correctly
Ensures data arrives in the correct order
Detects missing or duplicate data
Confirms data delivery
TCP is used in applications where accuracy is important, such as:
Web browsing
Email
File downloads
Online logins
Problems TCP is designed to solve
TCP is designed to solve the following problems:
Packet loss
Out-of-order delivery
Duplicate packets
No delivery confirmation
TCP solves these problems using acknowledgements, sequence numbers, and retransmission.
What is the TCP 3-Way Handshake?
Before data transfer starts, TCP first creates a connection between the client and the server.
This connection setup process is called the 3-Way Handshake.
It ensures that:
Both sides are ready to communicate
Both sides agree on starting data numbers
TCP 3-Way Handshake Diagram



TCP 3-Way Handshake (Step-by-Step)
Step 1: SYN
Client sends a SYN (synchronize) message to the server.
Meaning: “I want to start a connection.”
Step 2: SYN-ACK
Server replies with SYN-ACK.
Meaning: “I received your request and I am ready.”
Step 3: ACK
Client sends ACK.
Meaning: “Connection established.”
How data transfer works in TCP
Once the connection is established:
Data is divided into small segments
Each segment has a sequence number
Receiver sends ACK after receiving data
Sender sends the next segment
TCP Data Transfer Diagram



How TCP ensures reliability, order, and correctness
Reliability
Lost data is retransmitted.
Order
Sequence numbers arrange data correctly.
Correctness
Corrupted data is discarded and resent.
How TCP handles packet loss
If a packet is lost:
Receiver detects missing sequence number
ACK is not sent
Sender retransmits the lost packet
Packet Loss and Retransmission Diagram


How a TCP connection is closed
When communication is finished, TCP closes the connection safely.
Connection Termination Steps
One side sends FIN
Other side sends ACK
Other side sends FIN
Final ACK is sent
TCP Connection Close Diagram



TCP Connection Lifecycle
Connection establishment
Data transfer
Error handling and retransmission
Connection termination
Summary
TCP provides reliable communication
It ensures ordered and correct data delivery
Uses 3-way handshake to start connection
Uses sequence numbers and ACKs for data transfer
Retransmits lost packets
Closes connection safely using FIN and ACK




