What is the difference between TCP and UDP?

TCP and UDP are two transport-layer protocols used for sending data between hosts. Key differences:

  1. Connection model
  • TCP (Transmission Control Protocol) is connection-oriented: it establishes a connection (three-way handshake) before data transfer and closes it afterward.
  • UDP (User Datagram Protocol) is connectionless: packets (datagrams) are sent without setup.
  1. Reliability
  • TCP is reliable: it guarantees delivery, orders segments, and retransmits lost data using acknowledgements and sequence numbers.
  • UDP is unreliable: no delivery guarantees, no retransmissions, and packets may arrive out of order or be duplicated.
  1. Ordering and stream vs message
  • TCP provides a byte-stream abstraction with in-order delivery.
  • UDP preserves message boundaries: each datagram is independent; ordering is not guaranteed.
  1. Flow and congestion control
  • TCP implements flow control (sliding window) and congestion control (e.g., slow start, congestion avoidance).
  • UDP has no built-in flow or congestion control; the application must handle it if needed.
  1. Overhead and latency
  • TCP has higher overhead (headers, state, ACKs, retransmissions) and can add latency due to retransmit and congestion control.
  • UDP has lower overhead (smaller header, no connection state) and typically lower latency — useful for real-time apps.
  1. Header size and simplicity
  • TCP header is larger and more complex (minimum 20 bytes plus options).
  • UDP header is small and simple (8 bytes).
  1. Use cases
  • TCP: web pages (HTTP/HTTPS), email (SMTP), file transfer (FTP), where reliability and order matter.
  • UDP: DNS queries, streaming media, VoIP, online gaming, real-time telemetry — where low latency and reduced overhead are more important than guaranteed delivery.
  1. Error checking
  • Both have checksums; TCP’s checksum covers header and payload. UDP has an optional checksum in IPv4 (mandatory in IPv6).

When to use which: choose TCP when you need reliable, ordered delivery and can tolerate extra overhead; choose UDP when you need speed, low latency, or want to implement custom reliability/ordering at the application layer.

If you want protocol examples or packet diagrams (handshake, retransmit), say so.

Have your own question?

Ask the AI now