📡 TCP & UDP

Transport layer (L4) protocols — reliable delivery vs. fast transmission.

TCP and UDP are the two core transport layer protocols in the TCP/IP stack . They deliver data from applications to the network using ports to identify specific services.

⚖️ TCP vs UDP

🛡️ TCP

Connection-oriented — establishes a session before sending data.

✔️ Reliable — ACKs and retransmits lost packets

✔️ Ordered — sequence numbers reassemble data

✔️ Flow control — window size avoids overflow

❌ Higher overhead, slower

Use: HTTP , SMTP , SSH , FTP

⚡ UDP

Connectionless — sends data without prior negotiation.

❌ Unreliable — no ACKs, no retransmission

❌ No ordering — packets may arrive out of sequence

❌ No flow control — sender can flood receiver

✔️ Minimal overhead, very fast

Use: VoIP, gaming, streaming, DNS , DHCP

🤝 TCP three-way handshake

Before sending data, TCP establishes a connection using three packets. This ensures both sides are ready and synchronizes sequence numbers.

Client Server SYN seq=x SYN+ACK seq=yack=x+1 ACK seq=x+1ack=y+1
  1. SYN: Client sends SYN flag with initial sequence number (ISN)
  2. SYN+ACK: Server responds with SYN+ACK, acknowledging client's ISN and sending its own
  3. ACK: Client ACKs the server's ISN, connection is now established

📋 TCP header structure

The TCP header contains fields that enable reliability, ordering, and flow control. Each field serves a specific purpose in the protocol's operation.

TCP header structure diagram — insert image here

Source Port (16) | Dest Port (16) | Seq Number (32) | ACK Number (32) |
Data Offset (4) | Flags (9) | Window (16) | Checksum (16) | Urgent Pointer (16)

🧩 Source / Dest Port

Identifies sending and receiving applications. Common ports: 80 , 443 , 22 .

🔢 Sequence Number

Number of the first byte in this segment. Used to reassemble data in correct order.

✅ ACK Number

Confirms receipt of all bytes up to this number. Enables retransmission of lost data.

🚩 Flags

Control bits: SYN (start), ACK (acknowledge), FIN (finish), RST (reset), PSH (push).

📊 Window Size

How much data the receiver can accept — used for flow control.

✔️ Checksum

Error-checking field. If corrupted, the segment is discarded and retransmitted.

🔄 TCP state machine

A TCP connection transitions through several states during its lifetime. The diagram shows the complete state machine for both client and server sides.

TCP state machine diagram — insert image here

CLOSED → LISTEN → SYN_SENT → ESTABLISHED → FIN_WAIT → TIME_WAIT → CLOSED
(with transitions triggered by SYN, ACK, FIN, RST)

📋 UDP header structure

UDP is significantly simpler than TCP. Its header has only four fields, making it lightweight and fast.

UDP header structure diagram — insert image here

Source Port (16) | Dest Port (16) | Length (16) | Checksum (16)

🧩 Source / Dest Port

Identifies sending and receiving applications — same as TCP.

📏 Length

Total size of UDP datagram (header + data), in bytes.

✔️ Checksum

Error-checking field (optional in IPv4, required in IPv6).

💡 Key insight: TCP is like a phone call — you dial, wait for an answer, speak, say goodbye. UDP is like a smoke signal — you send it and hope someone sees it. Choose based on whether you need guaranteed delivery or low latency.

📖 Additional details

🔄 TCP retransmission & timeouts

TCP uses a Retransmission Timeout (RTO) — if an ACK isn't received within this time, the segment is resent. The RTO is dynamically calculated based on RTT (Round-Trip Time) measurements using algorithms like Karn's algorithm and Jacobson's algorithm.

After multiple failed retransmissions, TCP assumes the connection is broken and RST (reset) the connection.

🚀 TCP congestion control

TCP implements congestion control to avoid overwhelming the network. Key mechanisms include:

These algorithms are defined in various RFCs (e.g., RFC 5681 for TCP congestion control).

📌 UDP use cases in detail

UDP is preferred when:

Note that QUIC (used by HTTP/3) is built on UDP but adds reliability and encryption — showing that UDP can be used as a foundation for modern protocols.

📌 Note: Both TCP and UDP use ports for multiplexing. A port number (0–65535) identifies the specific application on a host. Standardized ports (0–1023) are reserved for well-known services.

🔗 Related topics

Deepen your understanding by exploring: IP — the network layer, Ports — how applications are identified, DNS — a classic UDP use case, HTTP — built on TCP, Firewalls — how they filter TCP/UDP traffic, TCP/IP stack — the complete architecture.