Skip to main content
Be Bitwise

TCP & UDP

Computer Networking Basics · Networking & Cryptography track

5 min read

In this module

The Transport Layer

IP gets packets from one machine to another, but it makes no promises. Packets can arrive out of order, get duplicated, or vanish into the ether. The transport layer sits on top of IP and decides how to handle that mess. Two protocols dominate: TCP and UDP.

TCP: Reliable Delivery

TCP (Transmission Control Protocol) guarantees that data arrives completely, in order, and without corruption. It does this through a set of mechanisms:

  • Sequence numbers - every byte of data is numbered so the receiver can reassemble them in order.
  • Acknowledgments - the receiver confirms what it has received; anything unacknowledged gets retransmitted.
  • Flow control - the receiver tells the sender how much data it can handle, preventing buffer overflows.
  • Congestion control - the sender also limits itself to what the network can carry, starting slowly, ramping up while ACKs keep arriving and backing off sharply when packets get lost.

Before any data flows, TCP performs a three-way handshake:

This setup adds latency but ensures both sides are ready. Closing is a separate exchange: each side sends a FIN when it has nothing more to say and the other acknowledges it, usually four segments in all.

Below is the client's first segment as raw TCP header fields: Offset/Flags is 5002, meaning a 20-byte header (the 5) with only the SYN bit set (02). Turn it into the server's reply. Set the flags to 5012 (SYN plus ACK, 0x10), pick any sequence number of your own, and set the Ack Number to 00000002, one more than the client's sequence number of 1.

Network Packet Builder

20 bytes
Hex Dump
c000005000000001000000005002ffff00000000

Click a layer to expand fields. Hover a field to highlight its bytes.

Sequence and acknowledgement numbers also drive retransmission. Each ACK names the next byte the receiver expects, so when a segment goes missing the sender's ACKs stop advancing, a retransmission timer runs out, and the sender tries again:

Real TCP usually recovers faster than this. If later segments do arrive, the receiver keeps repeating the same ACK, and three duplicates trigger a resend before the timer fires.

Predict: the client sends three 100-byte segments starting at seq=1001, and the second one is lost. What does each ACK from the server say, and what does the final ACK say once the missing segment is resent?

The first segment (bytes 1001-1100) arrives, so the server sends ack=1101. The second segment never turns up. When the third one (bytes 1201-1300) arrives, the server can't acknowledge it, because ACKs are cumulative: they name the next byte expected, and that's still 1101. So it sends ack=1101 again, a duplicate. Most receivers keep the out-of-order third segment in a buffer rather than throwing it away. When the resent second segment finally arrives, the gap closes and the server jumps straight to ack=1301, covering both.

Worked Example: One Short Connection

Here's a complete conversation with real numbers. The client wants to send a 78-byte HTTP request; the server replies with 512 bytes. Each side picks its own random initial sequence number (ISN). Say the client picks 1000 and the server 5000 (illustrative values; real ones are 32-bit and unpredictable).

#DirectionFlagsseqackDataWhat it means
1C → SSYN1000-0"My numbering starts at 1000"
2S → CSYN, ACK500010010"Mine starts at 5000; I got your SYN"
3C → SACK100150010"Got yours too"
4C → SACK, PSH1001500178Request bytes 1001-1078
5S → CACK, PSH50011079512Response bytes 5001-5512, and "send byte 1079 next"
6C → SACK, FIN107955130"Got all 512; I'm done sending"
7S → CACK, FIN551310800"Got your FIN; I'm done too"
8C → SACK108055140Connection closed

Two details trip people up. SYN and FIN each use up one sequence number even though they carry no data, which is why the ACK in row 2 is 1001 and the one in row 7 is 1080. And the server has folded its own FIN into its ACK in row 7, so this close takes three segments rather than the usual four.

Ports

A port is a 16-bit number (0-65535) that identifies a specific application on a machine. IP addresses get packets to the right computer; ports get them to the right program. For example, web servers typically listen on port 80 (HTTP) or 443 (HTTPS).

UDP: Speed Over Safety

UDP (User Datagram Protocol) is the bare-minimum alternative. It fires off packets (called datagrams) with no handshake, no acknowledgment, no ordering guarantees. If a packet is lost, UDP shrugs. Out-of-order packets? The application's problem now.

The header shows how little UDP does. It's 8 bytes and four fields: source port, destination port, length and a checksum. TCP's header is at least 20 bytes, because it has to carry sequence and acknowledgement numbers, flags and a window size on every segment.

Why would you ever want that? Sometimes speed matters more than completeness. Video calls, live game state updates, and DNS lookups all run on UDP. A few dropped packets in a video stream cause a brief glitch; waiting for retransmissions would freeze the picture for everyone.

Choosing Between Them

FeatureTCPUDP
ReliabilityGuaranteedBest effort
OrderingGuaranteedNot guaranteed
Connection setupThree-way handshakeNone
OverheadHigherLower
Typical usesWeb, email, file transferStreaming, gaming, DNS

Most of your daily traffic (web pages, messages, downloads) rides on TCP. The exception is growing: HTTP/3 runs over QUIC, a protocol that rebuilds TCP-style reliability and encryption on top of UDP, and major browsers and sites already use it. UDP wins in real-time scenarios where a millisecond saved beats a packet recovered.