Skip to content

TCP vs UDP

Both are Layer 4 (Transport Layer) protocols, but designed for different needs.

TCP (Transmission Control Protocol)

  • Connection-oriented — establishes connection before data transfer.
  • Reliable — guarantees delivery.
  • Ordered — data arrives in sequence.
  • Error checking — retransmits lost packets.
  • Flow control — prevents overwhelming receiver.
  • Congestion control — prevents overwhelming network.
  • Slower than UDP due to overhead.

TCP 3-Way Handshake (Connection Establishment)

Client                                Server
  |                                      |
  |------- SYN ------------------------>|  "I want to connect, seq=x"
  |                                      |
  |<------ SYN-ACK ----------------------|  "OK, seq=y, ack=x+1"
  |                                      |
  |------- ACK ------------------------->|  "Got it, ack=y+1"
  |                                      |
  |         Connection Established       |
  • SYN — synchronize sequence numbers
  • SYN-ACK — server acknowledges and sends its own SYN
  • ACK — client acknowledges server's SYN

TCP 4-Way Handshake (Connection Termination)

Client                                Server
  |                                      |
  |------- FIN ------------------------>|  "I'm done sending"
  |                                      |
  |<------ ACK ---------------------------|  "Got it"
  |                                      |
  |<------ FIN ---------------------------|  "I'm done too"
  |                                      |
  |------- ACK ------------------------->|  "Got it, bye!"
  |                                      |

4 steps because each direction closes independently.

TCP Header (20 bytes minimum)

  • Source/Destination Port
  • Sequence Number — tracks order of bytes
  • Acknowledgement Number — confirms received bytes
  • Flags — SYN, ACK, FIN, RST, PSH, URG
  • Window Size — flow control
  • Checksum

UDP (User Datagram Protocol)

  • Connectionless — no handshake, just send.
  • Unreliable — no guarantee of delivery.
  • Unordered — packets may arrive out of order.
  • No retransmission — lost packets stay lost.
  • No flow/congestion control.
  • Faster than TCP — minimal overhead.
  • Lightweight — small header (8 bytes only).

UDP Header (8 bytes only)

  • Source Port
  • Destination Port
  • Length
  • Checksum

UDP Operation

Client                                Server
  |                                      |
  |------- Data ------------------------>|
  |------- Data ------------------------>|
  |------- Data ------------------------>|
  |                                      |
  |     No handshake, no ACK             |

TCP vs UDP — Side by Side

FeatureTCPUDP
ConnectionConnection-orientedConnectionless
ReliabilityReliableUnreliable
OrderingOrderedUnordered
SpeedSlowerFaster
Header size20 bytes8 bytes
Flow controlYesNo
Congestion controlYesNo
RetransmissionYesNo
Handshake3-way handshakeNone

When to Use Which?

Use TCP when: Data must arrive correctly; order matters. Examples: HTTP/HTTPS, FTP, SMTP/IMAP, SSH, Telnet.

Use UDP when: Speed matters more than reliability; small amount of loss is acceptable. Examples: DNS, DHCP, VoIP, Video streaming (YouTube, Netflix), Online gaming, TFTP.

TCPUDP
Like registered post — confirmed deliveryRegular post — just send it