Open In App

Client-Server Model

Last Updated : 27 Aug, 2025
Comments
Improve
Suggest changes
129 Likes
Like
Report

The Client-Server Model is a distributed architecture where clients request services and servers provide them. It underpins many modern systems, including websites, email, and cloud storage platforms.

How Does the Client-Server Model Work?

Client: A client is any device or software that initiates communication by requesting data or services from a server. Common client applications include:

Web browsers (e.g., Chrome, Firefox)

Email apps (e.g., Gmail, Outlook)

Server: A server is a powerful systems that listens for and responds to client requests by delivering data or performing tasks. Servers often handle multiple simultaneous client requests. Common server applications include:

  • Web Servers (e.g., Apache, Nginx)
  • Email Servers
  • Database servers
Client-Server-Model
Client Server Model

How the Browser Interacts With the Servers?

The process of interacting with servers through a browser involves several steps:

1. User Enters the URL (Uniform Resource Locator):
The user types a website address (e.g., www.example.com) into the browser's address bar.

2. DNS (Domain Name System) Lookup:
The browser contacts a DNS server to convert the domain into an IP address.

3. Establishing a Connection:
The browser sends an HTTP/HTTPS request to the server using the resolved IP address.

4. Server Responds:
The server sends back website files (HTML, CSS, JavaScript, images).

5. Browser Renders the Webpage

  • DOM interpreter: Processes HTML to structure the page.
  • CSS interpreter: Applies styles
  • JavaScript Engine: Adds interactivity (using JIT compilation for performance).
Client-Server-Model
Client Server Request and Response

How Client-Server Communication Works in C++

In C++, sockets are used for communication between the client and the server over a network.

Example:

Server Code (server.cpp)

C++
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <netinet/in.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    // Create socket
    server_fd = socket(AF_INET, SOCK_STREAM, 0);

    // Setup address
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);

    // Bind socket
    bind(server_fd, (struct sockaddr*)&address, sizeof(address));

    // Start listening
    listen(server_fd, 3);
    std::cout << "Server waiting for connection...\n";

    // Accept a client connection
    new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);

    // Read message from client
    read(new_socket, buffer, 1024);
    std::cout << "Client says: " << buffer << std::endl;

    // Send reply
    const char* reply = "Hello from server!";
    send(new_socket, reply, strlen(reply), 0);

    close(new_socket);
    close(server_fd);
    return 0;
}

Client Code (client.cpp)

C++
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};

    // Create socket
    sock = socket(AF_INET, SOCK_STREAM, 0);

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(8080);
    inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);

    // Connect to server
    connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    // Send message
    const char* hello = "Hello from client!";
    send(sock, hello, strlen(hello), 0);

    // Receive reply
    read(sock, buffer, 1024);
    std::cout << "Server says: " << buffer << std::endl;

    close(sock);
    return 0;
}

Real-World Examples of the Client-Server Model

Use Case

Client

Server

Function

Email

Gmail, Outlook

Gmail/Yahoo Mail Servers

Send/receive email messages

Web Browsing

Chrome, Firefox

Apache, Nginx

Access and display websites

Cloud Storage

PC, Mobile App

Google Drive, Dropbox Servers

Upload/download and sync files

Advantages of the Client-Server Model

  • Centralized Data Management: Easy to maintain and back up data.
  • Cost Efficiency: Clients require less processing power.
  • Scalability: Servers and clients can scale independently.
  • Security: Centralized security policies and authentication.
  • Data Recovery: Easier backup and restore from a single source.

Disadvantages of Client-Server Model

  • Client Vulnerability: Risk of malware if servers distribute unsafe files.
  • Server as a Target: Susceptible to DDoS (Denial of Service) attacks.
  • Data Spoofing: Unprotected data can be tampered with in transit.
  • MITM Attacks: Unsecured connections can be intercepted by attackers.

Explore