Understanding Transport Protocols: UDP vs TCP

Oct 17, 2024

Overview of Transport Protocols

Introduction to Transport Protocols

  • Two major transport protocols: UDP (User Datagram Protocol) and TCP (Transmission Control Protocol).
  • Importance of transport protocols in enabling multiple applications to use a single network connection.
  • Transport protocols operate at the transport layer of the five-layer structure of the internet:
    • Application Layer
    • Transport Layer (UDP and TCP)
    • Network Layer
    • Link Layer
    • Physical Layer

Function of the Transport Layer

  • Allows multiple applications to share a network connection simultaneously.
  • Uses 65,000 ports per network connection for applications.
  • Example: Application uses port 12437 to send a message to port 80 on another machine.
    • Messages wrapped in segments containing source and destination ports.

UDP (User Datagram Protocol)

Characteristics of UDP

  • Packet Size: Smaller than TCP (UDP headers: 8 bytes; TCP headers: 20 bytes).
  • Connectionless: No need to establish a connection before sending data.
  • Control over Data Transmission: More flexible timing for sending data.
  • Error Detection: Uses a 16-bit checksum, but lacks reliability; corrupted packets may be discarded without recovery.
  • No Packet Retransmission: Once sent, a packet is not resent if lost.
  • No In-Order Delivery: Packets may arrive out of order.
  • No Congestion Control: Packets are sent regardless of network congestion, leading to potential dropped packets.

Conclusion on UDP

  • Lightweight but not reliable for critical data transmission.

TCP (Transmission Control Protocol)

Characteristics of TCP

  • Connection-Based: Requires a three-way handshake to establish a connection before data transmission:
    1. Initiator requests connection.
    2. Acceptor replies.
    3. Initiator acknowledges connection establishment.
  • Delivery Acknowledgements: Receiver confirms receipt of data.
  • Retransmission: Lost packets are resent if acknowledgments are not received in time.
  • In-Order Delivery: TCP reorders packets if they arrive out of sequence.
  • Congestion Control: Adjusts data transmission based on network traffic to minimize packet loss.
  • Mandatory Checksum: Error detection is enforced, with checksums required for both IPv4 and IPv6.

Downsides of TCP

  • Larger Headers: More overhead compared to UDP due to acknowledgments and retransmissions.
  • Latency: Congestion control can introduce delays.
  • Higher Overhead: Useful for applications that require reliable transmission but may hinder real-time communication (e.g., voice calls).

Conceptual Differences

  • UDP: Message-oriented, data sent in discrete chunks (like emails or texts).
  • TCP: Stream-oriented, data sent as a continuous flow, with TCP managing segmentation and reassembly.

Choosing Between UDP and TCP

Application Scenarios

  • Text Communication: Prefer TCP for reliability and in-order delivery.
  • File Downloads: Use TCP for retransmission and in-order delivery.
  • Remote Access Protocols (e.g., SSH): TCP is required for acknowledgments.
  • Multimedia Streaming: Ambiguous choice:
    • Traditionally UDP for lower overhead and tolerance for lost packets.
    • Increasing use of TCP when bandwidth allows, for better reliability.
  • DNS Lookups: UDP is ideal due to minimal overhead in connection establishment.

Summary

  • The choice between UDP and TCP depends on the application needs for reliability, speed, and data integrity.
  • Each protocol has its strengths and weaknesses suited for different use cases.