Transport Layer: TCP vs. UDP – A Deep Dive

1. Transport Layer Operation

The main features of the transport layer are:

  • Error Management: Handles and manages transmission errors.
  • Multiplexing: Enables multiple applications to share a single network card by sending different data streams.
  • Application Tracking: Uses port numbers to identify applications. Incoming packets are directed to the correct application based on their port number.
  • Data Segmentation: Divides raw data into smaller segments for transmission. Each segment is numbered for reassembly at the receiver.
  • Speed Control (TCP): Adapts the transmission rate to match the receiver’s speed, ensuring reliable delivery.

Conclusion: You can choose between speed (UDP) and reliability (TCP) based on application requirements.

2. TCP and UDP Headers

Key differences between TCP and UDP headers:

  • Size: UDP header (8 bytes) is smaller than TCP header (20 bytes).
  • Segment Number: TCP includes segment numbers for reassembly; UDP does not.
  • Checksum: TCP uses a checksum for error detection; UDP does not.
  • Control Bits: TCP has control bits for connection management; UDP does not.
  • Window Size: TCP uses a window size field for flow control; UDP does not.

3. Transmission Example

Here’s how a browser connects to a webpage:

  1. The browser initiates a connection to the destination port (e.g., port 80 for HTTP).
  2. The TCP layer creates a message with source and destination IP addresses and port numbers, along with the HTTP request (e.g., GET).
  3. The message is passed to the link layer, segmented, and sent to the physical layer.
  4. The physical layer transmits the data.
  5. The message reaches the server.
  6. The server’s physical layer reconstructs the data.
  7. The data moves up the layers to the transport layer.
  8. The transport layer checks the destination port and passes the message to the corresponding application.
  9. The application processes the request and sends a response.

4. Port-Level Routing

Similar to IP addresses, the transport layer uses port numbers to address applications. The netstat -a command can display open ports on a computer.

5. Port Number Allocation

  • Well-Known Ports (0-1023): Reserved for standard internet applications.
  • Registered Ports (1024-49151): Used by other applications.
  • Dynamic Ports (49152-65535): Assigned dynamically by the operating system.

6. Numbering and Reassembly (TCP)

TCP uses a three-way handshake for connection establishment:

  1. SYN (Synchronization) request
  2. SYN-ACK (Synchronization-Acknowledgment)
  3. ACK (Acknowledgment)

Key Difference between TCP and UDP: TCP includes segment numbers in each segment’s header for reassembly, while UDP does not.

7. TCP and UDP Header Contents

Both TCP and UDP headers include:

  1. Source Port
  2. Destination Port
  3. Source IP
  4. Destination IP

TCP also includes a sequence number and checksum.

8. TCP Features

  • Three-way handshake for connection establishment.
  • Connection termination process.
  • Segment acknowledgment.
  • Congestion control mechanism.
  • Timers for data loss detection.
  • Block acknowledgment to avoid overload.
  • Receiver buffering.
  • Segment reordering.
  • Missing segment retransmission.
  • Duplicate segment discarding.

9. UDP Features

UDP prioritizes performance over reliability. It uses the same port numbering mechanism as TCP.