Client-Server Architecture
Client-Server Architecture is a fundamental model used in networking and web systems. It explains how different computers communicate and share resources.
What Is Client-Server Architecture
Client-Server Architecture is a system where:
The client and server are separate systems connected over a network.
Simple idea:
Client → Sends Request
Server → Sends Response
What Is A Client
A Client is a device or program that requests data or services.
Examples of clients:
The client initiates communication.
What Is A Server
A Server is a computer or program that:
Servers are usually powerful computers that run continuously.
Examples of servers:
Basic Communication Flow
Here is the general process:
Step 1: Client sends a request.
Step 2: Server receives the request.
Step 3: Server processes the request.
Step 4: Server sends a response.
Step 5: Client displays or uses the response.
Example in web browsing:
You type a website URL.
Browser (client) sends request.
Web server sends back HTML page.
Browser displays it.
Real-World Analogy
Think of a restaurant.
Customer → Client
Waiter → Network
Kitchen → Server
Customer orders food.
Kitchen prepares food.
Food is delivered back to customer.
The kitchen does not ask customers for orders.
The customer starts the interaction.
Types Of Client-Server Systems
Two-Tier Architecture
Client communicates directly with server.
Example:
Browser ↔ Web Server
Three-Tier Architecture
Client → Application Server → Database Server
Example:
Browser → Backend Server → Database
This is common in modern web applications.
Advantages Of Client-Server Architecture
Centralized Control
Data is stored on the server.
Security
Server can control access and permissions.
Scalability
Servers can be upgraded to handle more clients.
Maintenance
Updates are done on the server, not every client.
Disadvantages Of Client-Server Architecture
Server Dependency
If server fails, clients cannot access services.
Network Dependency
Requires stable network connection.
Cost
Servers and infrastructure can be expensive.
Example Of Client-Server Communication Using Python
Below is a simple example of a server and a client using Python sockets.
Server Code:
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("localhost", 5000))
server_socket.listen(1)
print("Server is listening...")
conn, addr = server_socket.accept()
print("Connected by", addr)
data = conn.recv(1024)
print("Received:", data.decode())
conn.sendall(b"Hello from server")
conn.close()
Client Code:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))
client_socket.sendall(b"Hello from client")
data = client_socket.recv(1024)
print("Received:", data.decode())
client_socket.close()
How It Works:
This demonstrates the basic client-server model.
Common Protocols Used In Client-Server Architecture
HTTP
Used for web communication.
FTP
Used for file transfer.
SMTP
Used for sending emails.
DNS
Used for domain name resolution.
All these follow the client-server model.
Stateless Vs Stateful Servers
Stateless Server
Does not remember previous requests.
Example:
HTTP is mostly stateless.
Stateful Server
Remembers client information between requests.
Example:
Online games, database sessions.
Modern Client-Server Example
When you log into a social media platform:
Client:
Your browser or app sends login details.
Server:
Authenticates credentials.
Checks database.
Returns success or failure.
Stores session.
All major websites use client-server architecture.
Client-Server Vs Peer-To-Peer
Client-Server:
Central server handles requests.
Peer-To-Peer:
Each computer can act as both client and server.
Example of peer-to-peer:
File sharing networks.
Client-server is more common for web applications.
# Server Code
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("localhost", 5000))
server_socket.listen(1)
print("Server is listening...")
conn, addr = server_socket.accept()
print("Connected by", addr)
data = conn.recv(1024)
print("Received:", data.decode())
conn.sendall(b"Hello from server")
conn.close()
# Client Code
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))
client_socket.sendall(b"Hello from client")
data = client_socket.recv(1024)
print("Received:", data.decode())
client_socket.close()