UDP & TCP

Master the fundamental transport layer protocols that power internet communication

25 min read
Not Started

Protocol Performance Calculator

1024 bytes
10 MB/s
50 ms
0.1%
100 connections

Performance Metrics

Protocol Overhead:3.91%
Effective Data Rate:10 MB/s
Packets/Second:10656
Total Latency:200 ms

Protocol Features

Guaranteed Delivery:Yes
Ordered Delivery:Yes
Flow Control:Yes
CPU Overhead:High

Resource Usage

Memory Usage:6.25 MB
Connection Overhead:400 KB
Window Size:64 KB

Transport Layer Protocols

TCP and UDP are the fundamental transport layer protocols that enable communication between applications across networks. Understanding their trade-offs is crucial for designing performant distributed systems.

TCP (Transmission Control Protocol)

Reliable, connection-oriented protocol with guaranteed delivery, ordering, and flow control. Higher overhead but ensures data integrity.

UDP (User Datagram Protocol)

Fast, connectionless protocol with minimal overhead. No delivery guarantees but excellent for real-time applications.

TCP: Reliable Communication

Connection Management

Three-Way Handshake

Client → Server: SYN
Server → Client: SYN-ACK
Client → Server: ACK
# Connection established

Connection Termination

Client → Server: FIN
Server → Client: ACK
Server → Client: FIN
Client → Server: ACK

Flow Control & Congestion Control

Sliding Window

Controls how much data can be sent before receiving acknowledgment.

Window Size: Dynamic (up to 64KB)
Purpose: Prevent receiver overflow

Congestion Algorithms

Slow Start: Exponential growth
Congestion Avoidance: Linear growth
Fast Recovery: Quick adaptation

TCP Header Structure

Source Port
Dest Port
Sequence Number
Acknowledgment Number
Window Size
+ Control Flags, Checksum, Options
Minimum 20 bytes, Maximum 60 bytes

UDP: Fast Communication

Connectionless Design

UDP sends data without establishing a connection, making it much faster but less reliable than TCP.

✓ Advantages:
  • Low latency (no handshake)
  • Minimal protocol overhead
  • No connection state
  • Multicast/broadcast support
# UDP communication
socket.sendto(data, address)
# No handshake needed
# Immediate transmission
data = socket.recvfrom(1024)

UDP Header Structure

Source Port
Dest Port
Length
Checksum
Fixed 8 bytes
Simple & Efficient
Only 8 bytes of overhead vs TCP's 20+ bytes
Maximum Datagram
65,507 bytes (64KB - IP/UDP headers)
Checksum
Optional error detection (not correction)

Use Cases for UDP

🎮

Gaming

Low latency position updates, some packet loss acceptable

📺

Video Streaming

Real-time media where speed > perfect quality

🌐

DNS Queries

Simple request-response, fast lookups

TCP vs UDP Comparison

FeatureTCPUDP
ConnectionConnection-orientedConnectionless
ReliabilityGuaranteed deliveryBest effort
OrderingOrderedNo guarantee
Error DetectionYes + CorrectionBasic checksum
Flow ControlYesNo
Congestion ControlYesNo
Header Size20-60 bytes8 bytes
SpeedSlowerFaster
BroadcastingNoYes
Use CasesWeb, Email, File TransferGaming, Video, DNS, IoT

Real-World Protocol Usage

TCP Applications

HTTP/HTTPS

Web browsing requires reliable delivery of complete pages

Database Connections

SQL queries must be delivered completely and in order

File Transfers

FTP, SFTP ensure every byte is transferred correctly

Email (SMTP)

Message delivery must be guaranteed and complete

UDP Applications

Online Gaming

Player positions updated 60+ times per second

Live Video Streaming

Netflix, YouTube Live prefer current frames over old ones

Voice over IP (VoIP)

Real-time audio where slight drops are acceptable

IoT Sensors

Temperature readings sent frequently, occasional loss OK

Implementation Examples

TCP Server (Python)

import socket
# Create TCP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 8080))
server.listen(5)
while True:
client, addr = server.accept()
# Reliable connection established
data = client.recv(1024)
client.send(b"HTTP/1.1 200 OK\\r\\n")
client.close()

UDP Server (Python)

import socket
# Create UDP socket
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(('localhost', 8080))
while True:
# No connection needed
data, addr = server.recvfrom(1024)
# Process data immediately
response = process_data(data)
server.sendto(response, addr)

📝 UDP & TCP Knowledge Quiz

1 of 5Current: 0/5

What is the main difference between TCP and UDP?