Phase 1 – Day 16: Back To Basics – TCP
🔀
vs. UDP – Connection vs. Connectionless
Communication – A Deep Dive
🚀 Introduction
In computer networking, transport layer protocols play a crucial role in managing how data is
transmitted between devices. Two core protocols are TCP (Transmission Control Protocol) and
UDP (User Datagram Protocol). In this deep dive, we’ll explore both protocols from a practical,
real-world, and interview-ready perspective.
📦 TCP – Transmission Control Protocol
🧠 Key Characteristics:
● Connection-oriented (requires a 3-way handshake)
● Ensures reliable delivery of data
● Guarantees packet order and retransmits lost packets
● Performs error checking, flow control, and congestion control
📘 Real-World Use Cases:
● Web browsing (HTTP/HTTPS)
● File transfers (FTP, SFTP)
● Email services (SMTP, IMAP, POP3)
● Remote access (SSH, Telnet)
🔧 Troubleshooting Commands:
netstat -an | find "ESTABLISHED" # View active TCP connections
traceroute <hostname> # Trace TCP packet path
ping <host> # Use ICMP to test reachability
💨 UDP – User Datagram Protocol
🧠 Key Characteristics:
● Connectionless (no handshake)
● No delivery or ordering guarantees
● Lightweight and faster than TCP
● Suitable for real-time applications
📘 Real-World Use Cases:
● Video streaming (Netflix, YouTube)
● Online gaming (low latency)
● VoIP and conferencing (Zoom, Skype)
● DNS queries (fast, one-off messages)
🔧 Troubleshooting Commands:
nmap -sU -p 53,67,123 <host> # Scan for open UDP ports
iperf -u -c <host> # Test UDP performance
⚖️ TCP vs. UDP Comparison
Feature TCP UDP
Connection Yes (3-way handshake) No
Reliability Guaranteed Not guaranteed
Ordering Preserved Not preserved
Speed Slower due to overhead Faster, lightweight
Packet Loss Retransmits lost packets No retransmission
Handling
Use Cases Web, Email, File Transfer Video, Voice, DNS, Gaming
🛡️ Best Practices
● Use TCP when data integrity is critical
● Use UDP for applications needing low latency
● Ensure firewalls allow required TCP/UDP ports
● In mixed environments, optimize transport layer at the application level (e.g., implement
retry logic in UDP apps)
⚙️ Advanced Tips & Tricks
● TCP tuning: adjust window size for better performance in high-latency networks
● Use QoS tagging to prioritize UDP traffic (especially VoIP)
● Use Wireshark to inspect packet behavior and retransmissions
📍 Real-Life Example – Gaming vs File Transfer
● Scenario 1 (Gaming):
○ Game data sent via UDP: Speed prioritized over reliability
○ Packet loss tolerable, as new updates overwrite old data
● Scenario 2 (File Transfer):
○ File data sent via TCP: Every bit matters
○ Retransmits any lost data to ensure full file integrity
📘 Interview Questions and Answers
Beginner:
Q1. What’s the difference between TCP and UDP?
A: TCP is connection-oriented and ensures delivery; UDP is connectionless and faster but less
reliable.
Q2. What is a 3-way handshake in TCP?
A: It's the process of SYN, SYN-ACK, ACK used to establish a TCP connection.
Q3. Why is UDP used in DNS?
A: DNS queries are small and need fast delivery, making UDP ideal.
Intermediate:
Q4. What is TCP retransmission and when does it happen?
A: It occurs when packets are lost or corrupted. TCP resends them to maintain reliability.
Q5. Can UDP ensure reliability?
A: Not inherently. However, reliability can be added at the application layer (e.g., QUIC
protocol).
Advanced:
Q6. How does TCP handle congestion control?
A: Through mechanisms like slow start, congestion avoidance, fast retransmit, and fast
recovery.
Q7. How would you troubleshoot UDP packet loss?
A: Use tools like iperf and Wireshark, check buffer sizes, optimize network path, and prioritize
traffic via QoS.
🎯 Final Thoughts
TCP and UDP each serve unique roles in networking. The key is understanding when to use
what. TCP gives you guarantees, while UDP gives you speed.
Choosing wisely can be the difference between smooth streaming and buffering—or between a
successful file transfer and a corrupted download.