Computer Networks Essentials / Transport Layer

TCP vs UDP

๐Ÿค” Why Do We Need TCP and UDP?

Different applications have different requirements:

  • Some need accuracy & reliability โœ”๏ธ

  • Some need speed & low delay โœ”๏ธ

๐Ÿ“Œ A single protocol cannot satisfy both.

๐Ÿ‘‰ So, the Transport Layer provides two protocols:

  • TCP โ†’ Reliable

  • UDP โ†’ Fast


๐Ÿง  Basic Definitions

๐Ÿ“Œ TCP is a connection-oriented, reliable transport protocol that ensures error-free and ordered data delivery.

๐Ÿ“Œ UDP is a connectionless, fast transport protocol that sends data without guaranteeing delivery or order.


๐Ÿงญ Connection-Oriented vs Connectionless

๐Ÿ”— TCP โ€“ Connection-Oriented

  • Connection is established first

  • Data transfer happens

  • Connection is terminated

๐Ÿ“Œ Like a phone call ๐Ÿ“ž


๐Ÿ“ค UDP โ€“ Connectionless

  • No connection setup

  • Data sent directly

๐Ÿ“Œ Like sending a letter ๐Ÿ“ฎ


๐Ÿค TCP Connection Establishment (3-Way Handshake)

๐Ÿง  Steps:

1๏ธโƒฃ SYN โ†’ Request connection

2๏ธโƒฃ SYN-ACK โ†’ Acknowledge

3๏ธโƒฃ ACK โ†’ Connection established

๐Ÿ“Œ Ensures both sides are ready.

๐Ÿšซ UDP โ€“ No Handshake

  • No connection setup

  • No acknowledgment

  • No guarantee of delivery

๐Ÿ“Œ Faster, but risky.


๐Ÿ”น TCP

  • Segments numbered

  • Delivered in correct order

  • Reassembled properly

๐Ÿ”น UDP

  • No sequencing

  • May arrive out of order

๐Ÿ”น TCP

  • Error detection โœ”๏ธ

  • Retransmission โœ”๏ธ

  • Acknowledgments โœ”๏ธ

๐Ÿ“Œ Reliable delivery guaranteed.

๐Ÿ”น UDP

  • Error detection only โŒ

  • No retransmission โŒ

๐Ÿ“Œ Reliability not guaranteed.

๐Ÿ”น TCP

  • Uses sliding window

  • Prevents buffer overflow

๐Ÿ”น UDP

  • No flow control

  • Sender sends freely

  • TCP โ†’ Slower (overhead)

  • UDP โ†’ Faster (lightweight)

๐Ÿ“Œ UDP preferred for real-time communication.


๐Ÿ“ฆ Header Size

Protocol

Header Size

TCP

20โ€“60 bytes

UDP

8 bytes

๐Ÿ“Œ Smaller header = faster transmission.


๐Ÿง  TCP vs UDP โ€“ Detailed Comparison Table

Feature

TCP

UDP

Full Form

Transmission Control Protocol

User Datagram Protocol

Connection

Connection-oriented

Connectionless

Reliability

Reliable

Unreliable

Speed

Slower

Faster

Error Control

Yes

No

Flow Control

Yes

No

Congestion Control

Yes

No

Ordering

Maintained

Not maintained

Header Size

Large

Small

Acknowledgment

Required

Not required


๐ŸŒ Common Applications Using TCP

๐Ÿ“Œ TCP is used where accuracy matters:

  • ๐ŸŒ HTTP / HTTPS

  • ๐Ÿ“ง Email (SMTP, POP3)

  • ๐Ÿ“‚ File Transfer (FTP)

  • ๐Ÿ” Secure communication


๐ŸŽฅ Common Applications Using UDP

๐Ÿ“Œ UDP is used where speed matters:

  • ๐ŸŽฅ Live video streaming

  • ๐ŸŽฎ Online gaming

  • ๐Ÿ“ž VoIP

  • ๐Ÿ“ก DNS


๐Ÿง  Real-Life Example Comparison

Scenario

TCP

UDP

File download

โœ”๏ธ Yes

โŒ No

Live match streaming

โŒ No

โœ”๏ธ Yes

Online form submission

โœ”๏ธ Yes

โŒ No

Video call

โŒ No

โœ”๏ธ Yes


โš ๏ธ When to Use What?

โœ… Use TCP when:

  • Data must be correct

  • No data loss allowed

  • Order matters


โœ… Use UDP when:

  • Speed is critical

  • Minor data loss acceptable

  • Real-time communication


๐Ÿง  TCP & UDP in OSI Model

Both operate at:

  • Transport Layer (Layer 4)


๐Ÿง  Memory Tricks (Very Useful)

  • TCP = Trust, Control, Patience

  • UDP = Urgent, Direct, Performance


โญ One-Line Summary

๐Ÿ”น TCP provides reliable, ordered communication, while UDP provides fast, connectionless communication without guarantees.