A beginner-friendly and interview-ready guide to Client-Server Architecture, P2P, Protocols, TCP/UDP, WebSockets, and WebRTC.
Table of Contents
- Communication Models
- Protocols
- Network Layers
- TCP vs UDP
- HTTP vs WebSocket
- WebSocket vs WebRTC
- Interview Questions
Communication Models
1. Client-Server Architecture
In the Client-Server model, a client sends a request to a central server, and the server processes the request before sending back a response.
+-----------+
| Server |
+-----------+
/ | \
/ | \
/ | \
+---------+ +---------+ +---------+
| Client1 | | Client2 | | Client3 |
+---------+ +---------+ +---------+
Workflow
- Client sends Request
- Server processes Request
- Server returns Response
Examples
- Amazon
- Gmail
- Banking Applications
Key Points
- Centralized architecture
- Clients cannot communicate directly
- Every request passes through the server
2. Peer-to-Peer (P2P)
In Peer-to-Peer architecture, devices communicate directly with one another.
Peer A
/ \
/ \
Peer B ---- Peer C
Examples
- WebRTC
- Torrent
Key Points
- No central server for media transfer
- Direct communication between peers
- Lower latency
Protocols
A protocol is a set of rules that defines how data is transmitted between devices.
HTTP (HyperText Transfer Protocol)
Purpose
Used for loading websites and REST APIs.
Features
- Request → Response
- Stateless
- Runs over TCP
- Client always starts communication
Browser --------HTTP Request--------> Server
Browser <-------HTTP Response-------- Server
Used In
- Websites
- REST APIs
- Login Systems
- Web Applications
FTP (File Transfer Protocol)
Purpose
Transfers files between client and server.
FTP uses Two Connections
1. Control Connection
- Login
- Commands
- Authentication
2. Data Connection
- File Upload
- File Download
Client
│
├── Control Connection ─────────► Server
│
└── Data Connection ────────────► Server
Why Two Connections?
Commands and file data are transferred separately, making file transfer easier and more efficient.
SMTP (Simple Mail Transfer Protocol)
Purpose
Used to Send Emails.
Sender
↓
SMTP
↓
Mail Transfer Agent (MTA)
↓
Receiver Mail Server
Key Points
- Only sends emails
- Uses TCP
IMAP (Internet Message Access Protocol)
Purpose
Used to Read, Access and Synchronize Emails.
Mail Server
↓
IMAP
↓
Email Client
Key Points
- Reads emails
- Synchronizes emails across multiple devices
- Emails remain stored on the server
WebSocket
Purpose
Provides Real-Time Two-Way Communication.
Unlike HTTP, both client and server can send messages whenever they want.
Client 1
▲
│
▼
+-----------+
| Server |
+-----------+
▲ ▲ ▲
│ │ │
▼ ▼ ▼
C2 C3 C4
Important
- NOT Peer-to-Peer
- Client ↔ Server
- Full Duplex Communication
- Persistent Connection
Clients cannot communicate directly.
The server forwards messages between clients.
Examples
- WhatsApp Web
- Live Chat
- Multiplayer Games
- Notifications
- Stock Market Updates
WebRTC (Web Real-Time Communication)
Purpose
Allows direct Peer-to-Peer communication.
Peer A <──────────────► Peer B
Uses
- Video Calls
- Voice Calls
- Screen Sharing
Important
- Mostly uses UDP
- Optimized for low latency
- Supports audio, video and data channels
Network Layers
Only the important layers are shown.
+-----------------------+
| Application Layer |
+-----------------------+
| Transport Layer |
+-----------------------+
| Network Layer |
+-----------------------+
Application Layer
Contains protocols like:
- HTTP
- FTP
- SMTP
- IMAP
- WebSocket
Transport Layer
Responsible for
- Reliability
- Speed
- Packet Delivery
Protocols
- TCP
- UDP
Network Layer
Responsible for
- Routing
- IP Addressing
Uses IP Addresses to send packets between devices.
TCP (Transmission Control Protocol)
TCP is a Connection-Oriented protocol.
Before sending data, TCP establishes a connection.
Working
Connection Established
↓
Data Split into Packets
↓
Sequence Numbers Added
↓
Packets Sent
↓
Receiver Sends ACK
↓
Missing Packets Retransmitted
Features
- Reliable
- Ordered Delivery
- Acknowledgement (ACK)
- Retransmission
- Error Detection
- Connection-Oriented
Used In
- HTTP
- FTP
- Banking
- Downloads
When to Use TCP
Whenever every packet is important and must arrive in the correct order.
UDP (User Datagram Protocol)
UDP is a Connectionless protocol.
Working
Packet 1
Packet 3
Packet 2
Packet 5
Packets may arrive in any order.
Features
- No Connection Setup
- No ACK
- No Retransmission
- Best Effort Delivery
- Very Fast
Used In
- Live Streaming
- Video Calls
- Voice Calls
- Online Games
- DNS
When to Use UDP
When speed is more important than perfect reliability.
Example:
During a video call, losing one frame is acceptable, but waiting for retransmission would introduce lag.
TCP vs UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | ✅ Yes | ❌ No |
| Reliable | ✅ Yes | ❌ No |
| Ordering | ✅ Maintained | ❌ Not Guaranteed |
| ACK | ✅ Yes | ❌ No |
| Retransmission | ✅ Yes | ❌ No |
| Speed | Slower | Faster |
| Best For | Files, Banking, HTTP | Streaming, Gaming, Video Calls |
HTTP vs WebSocket
| HTTP | WebSocket |
|---|---|
| Request → Response | Full Duplex |
| Client starts communication | Both client and server can send anytime |
| Short-lived connection | Persistent connection |
| Not real-time | Real-time |
| Uses TCP | Uses TCP |
WebSocket vs WebRTC
| WebSocket | WebRTC |
|---|---|
| Client-Server | Peer-to-Peer |
| Uses TCP | Primarily Uses UDP |
| Chat Applications | Video Calls |
| Notifications | Voice Calls |
| Multiplayer Synchronization | Screen Sharing |
Which Protocol Should You Use?
| Situation | Protocol |
|---|---|
| Website | HTTP |
| File Transfer | FTP |
| Send Email | SMTP |
| Read Email | IMAP |
| Live Chat | WebSocket |
| Video Call | WebRTC |
| Banking | TCP |
| File Download | TCP |
| Live Streaming | UDP |
| Online Gaming | UDP |
Interview Questions
Why does FTP use two connections?
One connection is used for commands (Control Connection) and another is used for transferring files (Data Connection).
Why is UDP used in Video Calls?
Because speed is more important than reliability. Missing a few packets is better than introducing delay.
Why is TCP used in Banking?
Every packet must reach the destination correctly and in the correct order.
Is WebSocket Peer-to-Peer?
No.
WebSocket is a Client-Server protocol that supports Full Duplex communication.
Which protocol sends emails?
SMTP
Which protocol reads emails?
IMAP
Quick Revision
| Topic | Remember |
|---|---|
| HTTP | Request-Response |
| FTP | Two Connections |
| SMTP | Send Email |
| IMAP | Read Email |
| WebSocket | Full Duplex Client-Server |
| WebRTC | Peer-to-Peer |
| TCP | Reliable, Ordered |
| UDP | Fast, Best Effort |
| Live Streaming | UDP |
| Banking | TCP |
| Video Calls | WebRTC + UDP |
Summary
- HTTP → Websites and APIs
- FTP → File Transfer
- SMTP → Sending Emails
- IMAP → Reading Emails
- WebSocket → Real-time Client-Server Communication
- WebRTC → Peer-to-Peer Video/Audio Communication
- TCP → Reliable and Ordered
- UDP → Fast and Connectionless
Top comments (0)