Back to Blog

TCP vs UDP Explained

TCP vs UDP Explained
Command of the Day
ss -s

What it does: Displays a summary of total active, established, and closed TCP and UDP network sockets on the system.

At the Transport Layer (Layer 4) of the OSI model, two fundamental protocols govern how data is packaged and transmitted across IP networks: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).

Understanding how TCP and UDP operate under the hood is essential for configuring web servers, game servers, voice communications, and firewall rules on your RivinCloud VPS.


1. Core Differences Breakdown

       TCP (Connection-Oriented)                     UDP (Connectionless)
  Client                  Server                Client                  Server
    │── 1. SYN ─────────────►│                    │── 1. Datagram ────────►│
    │◄── 2. SYN-ACK ─────────│                    │── 2. Datagram ────────►│
    │── 3. ACK ─────────────►│                    │── 3. Datagram ────────►│
    │(Reliable, Ordered Data)│                    │(Fast, No Guarantee)    │

2. Direct Feature Comparison

Feature TCP (Transmission Control Protocol) UDP (User Datagram Protocol)
Connection State Connection-Oriented (Handshake) Connectionless (No handshake)
Data Delivery Guaranteed & Ordered Un-guaranteed (Best Effort)
Error Checking Extensive (Retransmits lost packets) Minimal (Checksum header only)
Header Overhead Larger Header (20 Bytes) Small Header (8 Bytes)
Traffic Control Flow Control & Congestion Control No Congestion Control
Speed / Latency Higher Latency (Handshake + ACK) Ultra-Low Latency

3. When to Use Each Protocol

Use TCP When Reliability is Critical:

  • Web Browsing (HTTP/HTTPS): Losing a single packet when loading an HTML or JavaScript file breaks execution.
  • SSH Terminal Access: Every keystroke must be verified and received in exact sequence.
  • Database & File Transfers (MySQL, SFTP): Data corruption from missing packets is un-acceptable.

Use UDP When Speed & Low Latency Matter Most:

  • Multiplayer Gaming (Minecraft, CS2, Valorant): Position updates must arrive instantly. Retransmitting an outdated player location from 200ms ago is useless.
  • Live Video & Voice Streaming (VoIP, Discord Audio, RTSP): Dropping a tiny audio frame is preferred over freezing playback to retransmit missing audio bits.
  • DNS Queries: Small request/response payloads where low latency is priority.

Command of the Day: ss

The ss (Socket Statistics) utility inspects open network sockets on Linux, replacing legacy netstat.

Syntax

ss [options]

Practical Example

# Display summary of all TCP and UDP sockets
ss -s

Sample Output

Total: 185
TCP:   12 (estab 4, closed 2, orphaned 0, timewait 2)

Transport Total     IP        IPv6
RAW       0         0         0
UDP       5         3         2
TCP       10        7         3

Explanation

This output reveals the active socket inventory, showing 10 TCP connections and 5 UDP sockets operating on the host.


Common Mistakes

  • Opening TCP ports in UFW when a service uses UDP: Game servers (like Minecraft Bedrock or TeamSpeak) use UDP. Allowing 25565/tcp in UFW will not open 25565/udp. Always specify the correct protocol (ufw allow 25565/udp).
  • Assuming TCP is always better: Using TCP for real-time multiplayer games causes head-of-line blocking lag when packet loss occurs.

Troubleshooting

  • Check listening UDP ports:
    sudo ss -ulpn
    
  • Check listening TCP ports:
    sudo ss -tlpn
    

Best Practices

  • Explicitly specify /tcp or /udp when configuring firewall rules.
  • Utilize UDP for real-time telemetry and streaming, and TCP for financial or data-store transactions.

Frequently Asked Questions

Can a service use both TCP and UDP on the same port?

Yes. DNS typically uses UDP port 53 for small queries, but falls back to TCP port 53 for large zone transfers exceeding 512 bytes.


Conclusion

TCP guarantees ordered data delivery, while UDP prioritizes low latency. Match the protocol to your application's requirements on RivinCloud VPS!