100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Difference Between TCP and UDP

TCP vs UDP compared — connection, reliability, ordering and latency — with use cases, code and networking interview questions answered.

easyQ1 of 224 in Computer Networks Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

TCP is a connection-oriented transport protocol that guarantees reliable, ordered, error-checked delivery through a handshake and acknowledgements, while UDP is a connectionless protocol that sends datagrams with no handshake, no ordering, and no delivery guarantee — trading reliability for lower latency.

TCP establishes a connection with a three-way handshake (SYN, SYN-ACK, ACK), numbers every byte so data arrives in order, retransmits lost segments, and applies flow and congestion control — which makes it ideal for web pages, email, and file transfer. UDP just fires datagrams with a tiny header and no state, so it is faster and lighter but may lose, duplicate, or reorder packets — which suits live video, voice, gaming, and DNS where speed matters more than perfect delivery. Interviewers use this to check you understand the reliability-versus-latency trade-off.

  • TCP: reliable, ordered, error-checked delivery
  • TCP: flow and congestion control prevent overload
  • UDP: low latency with minimal overhead
  • UDP: supports broadcast and multicast

AI Mentor Explanation

TCP is like a captain confirming every signal with the bowler before each ball — the sign is given, acknowledged with a nod, and only then does play continue, so nothing is missed and the order of deliveries is never confused. UDP is like a fielder shouting quick instructions across the ground: fast and continuous, but if the wind swallows a word nobody stops to repeat it. One guarantees every message lands in order; the other keeps the game moving even if a call is lost.

Step-by-Step Explanation

  1. Step 1

    Connection

    TCP does a three-way handshake first; UDP sends datagrams with no setup.

  2. Step 2

    Reliability

    TCP acknowledges and retransmits lost data; UDP does neither.

  3. Step 3

    Ordering

    TCP sequences bytes so they arrive in order; UDP may reorder or drop.

  4. Step 4

    Choose by need

    Correctness (web, email, files) → TCP; low latency (voice, video, DNS) → UDP.

What Interviewer Expects

  • Connection-oriented vs connectionless as the core split
  • How TCP achieves reliability (handshake, ACKs, retransmission)
  • The latency vs reliability trade-off
  • Correct example use cases for each

Common Mistakes

  • Saying UDP is always faster with no downside
  • Claiming TCP guarantees a fixed delivery time
  • Forgetting UDP has no ordering or retransmission
  • Not giving concrete use cases for each protocol

Best Answer (HR Friendly)

TCP is the careful, reliable option — it confirms every piece of data arrives and in the right order, which is why it powers websites and file downloads. UDP is the fast, lightweight option that just sends data without checking, which suits live video, calls, and gaming where speed matters more than the odd lost packet.

Code Example

TCP socket vs UDP socket in Python
import socket

# TCP: connection-oriented, reliable
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.connect(("example.com", 80))   # handshake happens here
tcp.sendall(b"GET / HTTP/1.0\r\n\r\n")

# UDP: connectionless, best-effort
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp.sendto(b"ping", ("example.com", 9999))   # no handshake, may be lost

Follow-up Questions

  • How does the TCP three-way handshake work?
  • What is TCP flow control versus congestion control?
  • Why does DNS mostly use UDP?
  • What is head-of-line blocking in TCP?

MCQ Practice

1. Which protocol is connection-oriented and reliable?

TCP establishes a connection and guarantees ordered, acknowledged delivery.

2. Which is the better fit for real-time voice calls?

UDP’s low latency suits real-time media, where a dropped packet is better than a delay.

3. The TCP connection setup is called the?

TCP opens a connection with SYN, SYN-ACK, ACK — the three-way handshake.

Flash Cards

TCP in one line?Connection-oriented, reliable, ordered delivery via handshake and ACKs.

UDP in one line?Connectionless, best-effort, low-latency datagrams with no guarantees.

When to use UDP?Live video, voice, gaming, DNS — speed over perfect reliability.

TCP handshake steps?SYN → SYN-ACK → ACK (three-way handshake).

1 / 4

Continue Learning