TCP (Transmission Control Protocol) is a connection-oriented, reliable transport layer protocol used for communication between devices on a network. It ensures that data is delivered accurately, in order, and without errors.
Key Features of TCP
1. Connection-Oriented
- Establishes a connection before transmitting data using the Three-Way Handshake.
- Ensures both sender and receiver are ready for communication.
2. Reliable Data Transmission
- Uses ACK (Acknowledgments) to confirm data receipt.
- Retransmits lost or corrupted packets.
3. Ordered Delivery
- Uses sequence numbers to ensure packets arrive in the correct order.
4. Flow Control (Sliding Window Protocol)
- Prevents sender from overwhelming the receiver.
- Adjusts the amount of data sent based on receiver capacity.
5. Congestion Control
- Prevents network congestion by adjusting transmission speed dynamically.
- Uses Slow Start, Congestion Avoidance, Fast Retransmit algorithms.
6. Error Detection & Correction
- Uses checksums to detect errors in transmitted data.
- Retransmits corrupted data.
How TCP Works
1. TCP Connection Establishment (Three-Way Handshake)
Before data transfer, TCP establishes a reliable connection between sender and receiver:
- SYN (Synchronize) β The client sends a SYN request to initiate communication.
- SYN-ACK (Synchronize-Acknowledge) β The server responds with a SYN-ACK.
- ACK (Acknowledge) β The client sends an ACK, completing the handshake.
πΉ Now, data transfer begins!
2. TCP Data Transmission & Reliability
πΉ How TCP Ensures Reliable Delivery:
- Segmentation β Large data is broken into smaller segments.
- Sequence Numbers β Each segment is assigned a number to track order.
- Acknowledgment (ACK) β Receiver confirms data is received.
- Retransmission β If an ACK is not received, TCP resends the data.
πΉ How TCP Handles Lost Packets:
- If a packet is lost, the sender detects this using timeouts or duplicate ACKs.
- Lost packets are retransmitted to ensure complete delivery.
3. TCP Connection Termination (Four-Way Handshake)
When communication is complete, TCP gracefully closes the connection:
- FIN (Finish) β One side requests to close the connection.
- ACK β The other side acknowledges.
- FIN β The second side also sends a FIN request.
- ACK β The first side acknowledges, and the connection is closed.
πΉ Now, the connection is terminated!
TCP Header Structure (Fields & Their Functions)
Field | Description |
---|---|
Source Port | Identifies the sender’s application. |
Destination Port | Identifies the receiver’s application. |
Sequence Number | Ensures correct order of data segments. |
Acknowledgment Number | Confirms receipt of data. |
Header Length | Specifies size of TCP header. |
Flags (Control Bits) | Controls connection setup and termination (e.g., SYN, ACK, FIN). |
Window Size | Specifies the receiverβs buffer size (flow control). |
Checksum | Ensures data integrity (error detection). |
Urgent Pointer | Indicates urgent data transmission. |
Options | Extra settings for performance tuning. |
TCP Flow Control: Sliding Window Protocol
To prevent data overflow, TCP adjusts how much data is sent based on receiver capacity.
πΉ How it Works:
- Sender transmits a “window” of data before waiting for an acknowledgment.
- Receiver acknowledges received data and updates the window size.
- Window size is dynamically adjusted based on network conditions.
TCP Congestion Control Mechanisms
πΉ TCP adjusts data transmission speed to avoid congesting the network.
1. Slow Start
- Starts with small data transmission and gradually increases.
- Prevents flooding the network initially.
2. Congestion Avoidance
- Monitors network congestion and reduces transmission speed if needed.
3. Fast Retransmit & Fast Recovery
- If duplicate ACKs are received, TCP immediately retransmits missing data.
- Helps recover from packet loss quickly.
TCP vs UDP
Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
---|---|---|
Connection Type | Connection-Oriented | Connectionless |
Reliability | Reliable (ACKs, retransmissions) | Unreliable (No ACKs) |
Ordering | Ensures correct order of packets | No guaranteed order |
Error Checking | Yes (Checksum, retransmission) | Yes (Checksum only) |
Flow Control | Yes (Sliding Window) | No |
Congestion Control | Yes (Slow Start, AIMD) | No |
Speed | Slower | Faster |
Best For | Web browsing, emails, file transfers | Streaming, gaming, VoIP |
Common Applications That Use TCP
β
Web Browsing (HTTP, HTTPS) β Ensures complete and correct webpage loading.
β
Email Services (SMTP, IMAP, POP3) β Reliable delivery of emails.
β
File Transfers (FTP, SFTP, SCP) β Ensures all files arrive intact.
β
Remote Access (SSH, Telnet) β Secure and stable connections for remote systems.
β
Database Communication (MySQL, PostgreSQL) β Prevents data loss in transactions.
Advantages of TCP
β Reliable & Secure β Guarantees data delivery and correct order.
β Error Handling β Detects and retransmits lost or corrupted packets.
β Flow & Congestion Control β Prevents data loss and network congestion.
β Widely Used β Supports most internet applications (web, email, file transfer).
Disadvantages of TCP
β Slower than UDP β Due to acknowledgments and retransmissions.
β Higher Overhead β TCP headers are larger (more control bits).
β Not Ideal for Real-Time Applications β Delays can affect video streaming or gaming.
Conclusion
TCP is the backbone of reliable internet communication, ensuring accurate, ordered, and error-free data transmission. While it has overhead, its reliability makes it essential for applications where data integrity matters.