TCP Protocol Guide

Transmission Control Protocol - Reliable Data Transmission

What is TCP?

TCP (Transmission Control Protocol) is a connection-oriented, reliable transport protocol that ensures data is delivered correctly and in order. It's one of the core protocols of the Internet.

Reliable

Guarantees data delivery with error checking and retransmission.

Connection-oriented

Establishes a connection before data transmission begins.

Ordered

Ensures data packets arrive in the correct sequence.

TCP Three-Way Handshake

Key TCP Features

Flow Control

Prevents overwhelming the receiver by controlling data transmission rate.

// Sliding Window Protocol
Window Size = min(receiver_buffer, network_capacity)
Sender waits for ACK before sending more data

Error Detection

Uses checksums to detect corrupted data and requests retransmission.

// Checksum Calculation
checksum = sum_of_all_bytes % 65536
if (checksum != received_checksum) {
request_retransmission();
}

Congestion Control

Manages network traffic to prevent network congestion and collapse.

// Slow Start Algorithm
cwnd = 1; // Congestion Window
while (cwnd < ssthresh) {
cwnd *= 2; // Exponential growth
}

Ordered Delivery

Sequences packets to ensure they arrive in the correct order.

// Sequence Numbers
seq_num = initial_seq + bytes_sent
receiver buffers out-of-order packets
delivers in sequence to application

TCP Header Structure

TCP vs UDP Comparison

Feature TCP UDP
Connection Connection-oriented Connectionless
Reliability High (guaranteed delivery) Low (best effort)
Ordering Guaranteed Not guaranteed
Speed Slower (overhead) Faster (minimal overhead)
Header Size 20 bytes (minimum) 8 bytes
Use Cases Web browsing, email, file transfer Gaming, streaming, DNS

TCP Connection States

Common TCP Ports

Web Services

  • 80 - HTTP
  • 443 - HTTPS
  • 8080 - HTTP Alt

Email

  • 25 - SMTP
  • 110 - POP3
  • 143 - IMAP

File Transfer

  • 21 - FTP
  • 22 - SSH/SFTP
  • 23 - Telnet

Database

  • 3306 - MySQL
  • 5432 - PostgreSQL
  • 1433 - SQL Server