Computer Networks / Lecture / Lecture 10
Lecture · Computer Networks

Client-server and peer-to-peer architecture

When you send a message on Telegram, it first goes to a server, while when you download a game through a torrent, the pieces of the file arrive at the same time from hundreds of peers. These two paths — client-server and P2P — are the foundation of almost every service on the Internet. Today you will measure in simulators how they are built and when each one wins.

:ik-kitob: Lecture 10 ⏱ 2 academic hours (80 minutes) 🚇 Packet simulators ⚡ One server or a thousand peers
10

Lesson goals and plan

By the end of the lesson you will…

  • explain the components of the client-server architecture and the “request → response” cycle
  • explain the concepts of a socket (IP:port), a server program and service ports with examples
  • explain the pure and hybrid types of P2P networks and the working principle of BitTorrent (pieces, seeder, tracker)
  • compare the two architectures by resource management, scalability, security and field of use
  • calculate file distribution time (Dcs and DP2P) with formulas and interpret the result
  • justify the choice of a suitable architecture for a real service (Telegram, Zoom, Steam, my.gov.uz…)

Plan (according to the syllabus)

  1. The structure and working principles of client-server and peer-to-peer network architectures
  2. Comparing the two architectures by resource management, scalability, security and field of use

In this lesson there are two “metro lines”: one takes every passenger to one central station (the server), and on the other every station carries passengers itself (P2P). Change the numbers in the simulators and find out for yourself when each line gets jammed.

Why does the official site “freeze” on the day a new game is released, while a torrent, on the contrary, gets faster?

On the official site, one server (or a group of servers) gives the file to everybody: the more downloaders there are, the lower the speed each of them gets. In a torrent, every downloader also passes on the pieces it has received to others: the more people there are, the more distributors there are. The same file, the same Internet — but a different architecture, and the result is completely the opposite. Today we will prove why with formulas and a simulator.

Key terms

First recall the meaning of the term yourself, then click the card to check.

The structure and working principles of client-server and peer-to-peer network architectures

A network architecture is a scheme that determines who contacts whom in programs. Even when the physical cables and routers are the same, applications can choose between two “cultures of communication”: centralized (client-server) or distributed (P2P).

CLIENT-SERVER SERVERalways on C1C2C3C4C5 clients do not talk to one another PEER-TO-PEER P1P2P3P4P5P6 every peer is both a client and a server
Figure 10.1. On the left — the centralized model (all roads lead to the central station), on the right — the distributed model (the stations are connected directly to one another).

Client-server architecture

Definition

The client-server architecture is a model in which the roles of a network application are strictly separated: the server, always switched on, waits for requests at a known address and port, and the clients send it a request when needed and receive a response. Clients usually do not communicate directly with one another.

A real-life example: you log in to HEMIS and look at your grades. Your browser (the client) sends the university server the request “give me this student’s grades”; the server takes the grades from the database and returns them as an HTML page. Your groupmate’s browser does not “know” your browser at all — both of you talk only to the server.

Main features of client-server

:ik-server:

Server

A permanent IP address or domain name, always switched on, serves many clients at the same time. In large services a single “server” is actually thousands of computers in a data center.

:ik-telefon:

Client

Connects from time to time, its IP address may change (at home, in the metro, at the university — all different), and it starts the communication itself.

The life path of one request

From the moment you type hemis.adu.uz in the browser and press Enter until the page opens, the following stages take place. Each of them is studied separately in later lessons — for now just look at the overall picture.

  1. Turning a name into an address (DNS)

    The client asks a DNS server for the IP address of the domain name. This is also a small client-server exchange (Lecture 15).

  2. Establishing a connection (TCP)

    The client asks to connect to the server’s port 443. On the client side the operating system chooses a temporary port, for example 51834 (Lecture 12).

  3. Sending the request

    A message like GET /baholar HTTP/1.1 is sent. It is split into packets and reaches the server through routers (Lecture 11).

  4. Processing

    The server program reads the request, checks permission, queries the database and prepares the response.

  5. Returning the response

    HTTP/1.1 200 OK and the text of the page return to the client; the browser draws it on the screen.

Sockets and ports: “which door should we knock on?”

A website, a mail service and a database can run on one server computer at the same time. Which program an arriving packet belongs to is determined by the port number. The IP address is the address of a multi-storey building, and the port is the apartment number. The IP + port pair is called a socket; and one TCP connection is identified by four values: client IP, client port, server IP, server port.

Table 10.1. Common services and their standard ports
ServiceProtocolPortExample from life
Web page (unencrypted)HTTP80old websites, a router’s settings page
Web page (encrypted)HTTPS443my.gov.uz, the Payme web account
Domain namesDNS53kun.uz → IP address
Remote administrationSSH22an administrator connecting to a server
Sending mailSMTP25 / 587sending a letter from a university mailbox
File transferFTP21uploading website files to a hosting service

Two-tier and three-tier structure

In the simple case the client talks directly to the server (two-tier). In large services like Uzum Market there are usually three tiers: (1) the client — an app or browser, (2) the application server — the business logic (the cart, discounts, the order), (3) the database server. The client never sees the database directly — this is important for security. In systems with many users a load balancer is placed in front of the application servers: it hands out requests in turn to several identical servers.

In the code the server waits inside a loop (while True) and answers every request that arrives — real servers also work in exactly this kind of “eternal loop”. 200, 404 and 405 are HTTP status codes; they are studied in detail in practical class 16.

Peer-to-Peer (P2P) architecture

Definition

Peer-to-peer (P2P) architecture is one in which the participants (peers) have equal rights, and each of them both gives and receives resources (file pieces, computing power, bandwidth) from the others. Dependence on an always-on central server is absent or minimal.

The best-known example is BitTorrent. The file is split into pieces of equal size (for example, 256 KB each). A newly joined peer downloads pieces from different peers and, as soon as it receives them, starts distributing them to others too. That is why the distribution “power” grows with every person who joins the network.

:ik-pazl:

Pieces

The file is divided into parts; every piece has a hash value — a corrupted or fake piece is detected immediately.

:ik-maqsad:

“Rarest first”

(rarest first) A peer first asks for the piece that is least common in the network — that way no piece “disappears”.

:ik-odamlar:

Tit-for-tat

A peer gives more to those who give it pieces the fastest. “Take-only” users download more slowly.

Pure and hybrid P2P

Table 10.2. Types of P2P networks
TypeWho knows what?Examples
Pure P2PThere is no central server at all; peers find one another by asking each other (a distributed hash table — DHT)BitTorrent in DHT mode, blockchain networks (Bitcoin nodes)
Hybrid P2PA central server serves only as a “directory”: who is online, who has what. The main data is transferred directly between peersBitTorrent with a tracker; in video calls, a server for setting up the connection + a media stream sent directly
Did you know?

When you make a video call through Telegram or WhatsApp, a server is used first to set up the connection, and then, if possible, the audio-video stream is sent directly (P2P) between the two devices. If home routers (NAT) do not allow a direct connection, the stream goes through a relay server. So both architectures live inside a single app.

Simulator: how are the pieces distributed?

Each row is one user, each cell is one piece of the file (8 pieces). The server can send 2 pieces per “tick”. In client-server mode only the server distributes; in P2P mode every peer that has a piece also gives one piece to another peer at each tick. Change the number of users, run both modes and compare the number of ticks.

:ik-pazl: Spread of pieces: is the server alone, or is everyone together?

Tick: 0 Mode: Client-server The results are collected here.

As you observed, in client-server mode the number of ticks grows in direct proportion to the number of users (close to N·8/2 = 4N), while in P2P mode the number of ticks hardly changes even when N grows. The red cells are the new pieces that arrived on that tick.

Comparing the two architectures by resource management, scalability, security and field of use

The correct answer to the question “which is better?” is “it depends on the task”. Let us compare them by four criteria and even measure scalability with a formula.

Table 10.3. Client-server and P2P: a comparison by four criteria
CriterionClient-serverPeer-to-Peer
Resource managementData and computation are at the center; backups, updates and control are in one place, easyResources are spread among peers; everyone manages their own computer, there is no single control
ScalabilityAs users grow, the server load grows → new servers, bandwidth and a data center are needed (expensive)Every new peer brings its own bandwidth to the system too → it “scales by itself”
ReliabilityIf the server goes down, the service stops (a single point of failure); reduced with backup serversThe network keeps working even if several peers leave; but if the owners of a rare file leave, the file may be lost
SecurityCentral authentication, permissions, logs, encryption — easy to manage. But the server is a target of attack (DDoS, break-ins)No central control: malicious peers, fake pieces, your IP address is visible to others, risk of malware spreading
CostThe service owner pays for servers, the data center and bandwidthThe cost is shared among the users
Field of useBanks, government services, social networks, mail, online shops, learning systemsDistributing large files, game updates, cryptocurrencies, video call streams, local file sharing

We measure scalability: file distribution time

Suppose a department wants to distribute a teaching video of size F bits to N students. The server’s upload speed is us, each student’s upload speed is u, and the download speed of the slowest student is dmin. The minimum time until all N students have received the whole file is the distribution time D.

Client-server

The server has to send N copies itself: N·F bits, at speed us. In addition, the slowest client receives F bits at speed dmin.

Dcs = max( N·F / us , F / dmin )

When N is large the first term dominates → the time grows linearly with N.

P2P

The server has to send at least one copy (F/us), and the total of N·F bits is spread using the sum of the upload speeds of everyone.

DP2P = max( F/us , F/dmin , N·F / (us + N·u) )

As N grows, the third term approaches F/u → the time is bounded.

Source

These formulas are a simplified form of the classic model given in the textbook by J. Kurose and K. Ross (reference 13, chapter 2): all peers are assumed to have the same upload speed. It is an estimate of the lower bound (the best case).

Graph simulator

Drag the sliders: the graph plots the distribution time for both architectures for N = 1…100. What happens if you bring the peer upload speed close to 0? And if you raise the server speed 10 times?

:ik-grafik: Distribution time: Dcs and DP2P

Now write the same calculation yourself in Python. Pay attention to the units: the file size is in megabytes (MB), the speeds are in megabits per second (Mbit/s) — 1 byte = 8 bits.

Resource management and security — in more depth

:ik-bino: Why will a bank never be P2P?

A money transfer in Payme or Click is an operation that requires a single, trusted ledger (who has how much money). In the client-server model this ledger is kept in one database under control, every operation is written to a log, and the user is checked with a password and an SMS code. In P2P the question arises: “whose word do we trust?” — blockchains solve it with complex consensus algorithms, but this is much slower and more expensive.

:ik-oyin: Why do game updates love P2P?

An update is a large file that is the same for everyone and needed by everyone at the same time. This is exactly the situation where P2P is strongest. That is why help from peers is used to deliver some game and system updates (for example, in Windows, the ability to get updates from computers on the local network — Delivery Optimization).

Table 10.4. Threats and ways of protection
ThreatIn which architecture is it dangerousProtection
DDoS — “choking” the server with requestsClient-server (a single target)Load balancing, CDN, traffic filtering
Breaking into the server’s database and stealing dataClient-serverEncryption, restricting permissions, updates
A fake or malicious piece (poisoning)P2PChecking every piece with a hash
Your IP address being visible to strangersP2PA trusted client program, only legal content
Spreading malware (disguised as a game “crack”)P2PAntivirus, official sources

Field of use: which service is in which model?

Finding the rare piece (rarest first)

How does a BitTorrent client decide which piece to ask for first? It counts the pieces held by the peers it knows and chooses the least common one. Write this algorithm.

Common mistakes

MisconceptionWhy it is wrongCorrect answer
“A server is a very powerful computer”A server is first of all a role and a program. A web server can be run even on an ordinary laptopA server is a program that waits for requests and answers them; the powerful computer set aside for it is also called that
“There is no server at all in P2P”Hybrid P2P has trackers and connection-setup (signaling) serversThere is none in pure P2P; in a hybrid there is a helper server, but the main data is between peers
“P2P is something illegal”The technology is being confused with its misuseLinux distributions, game updates and legal content are distributed through P2P; the illegality is in the content
“A port is a physical socket on a computer, like USB”A network port is a software numberA port is a number from 0 to 65535 that distinguishes a service
“P2P gets slower when users are added”A new peer adds its upload bandwidth tooP2P distribution time stays bounded around F/u even when N grows
“A computer is either only a client or only a server”A browser (client) and a local web server can run on one computer at the same timeA role belongs to a program, not to a computer

Reinforcement

// REMEMBER IN 1 MINUTE

  1. Client-server: the server is always on and waits, the client sends the request; clients do not talk to one another.
  2. Socket = IP + port; the port distinguishes the service (80, 443, 53, 22, 25).
  3. P2P: a peer both receives and gives; there are pure (DHT) and hybrid (tracker) types.
  4. Dcs = max(NF/us, F/dmin) — linear in N; the additional term of DP2P, NF/(us+Nu), is bounded.
  5. Central control, security, a single ledger needed → client-server; a large identical file for many people → P2P.
:ik-chat:TelegramMessages are stored on the server (cloud chats) — that is why everything is visible on a new phone too. This is client-server.
:ik-talaba:HEMISBrowser → application server → database: the classic three-tier structure.
:ik-oyin:Steam / gamesDownloading goes through content servers and a CDN; in some games the players connect to each other over P2P.

Final test

Review questions

What do you understand by the client-server architecture?

A model with strictly separated roles: the server, always on, waits for requests at a known address and port, and clients send requests and receive responses; clients usually do not communicate directly with one another.

List the main differences between a server and a client.

Server: always on, permanent address, serves many clients, waits for requests. Client: connects from time to time, its address changes, it starts the communication itself.

Why are ports and sockets needed?

Several services run on a computer with one IP address; the port shows which program a packet belongs to. IP + port = socket — the exact “door” of the communication.

List the stages of one web request.

Finding the IP address through DNS → TCP connection (port 443) → sending the request → processing on the server → returning the response and drawing the page.

What is the difference between pure and hybrid P2P?

In pure P2P there is no central server at all (peers find one another through DHT). In a hybrid, a central server serves only as an index/directory, and the main data is transferred between peers.

How does BitTorrent work?

The file is split into pieces; a peer finds other peers through a tracker or DHT, downloads pieces from them and distributes the ones it has received; rare pieces are requested first, exchange is encouraged with tit-for-tat, and every piece is checked with a hash.

Why does the client-server distribution time grow linearly with N?

The server has to send N copies itself: N·F bits at a constant speed us — the time is N·F/us.

What are the weaknesses of P2P in terms of security?

No central control, malicious or fake peers, your IP address being visible to others, the spread of malware, and the risk of copyright violation.

What is a “single point of failure” and how is it reduced?

A component whose failure stops the whole system (for example, a single server). It is reduced with backup servers, a load balancer and several data centers.

Give an example of both architectures being used together in one app.

A video call: setting up the connection and the contact list go through a server, while the audio-video stream goes directly between peers (P2P) if possible, otherwise through a relay server.

Independent work

The independent study topic in the course syllabus: “P2P, multimedia, VoIP, RTP/RTSP, CDN and cloud network services.” (6 hours). Analyze the service in your assigned variant and prepare a short report (2–3 pages):

  1. Which architecture does the service use (client-server, pure/hybrid P2P, mixed)? Justify it by relying on official sources or technical articles.
  2. Draw a structural diagram of the service (client, servers, CDN, peers; the directions of data flow).
  3. Calculate Dcs and DP2P with the parameters of the task (you may use the Python calculator from the lesson) and interpret the result.
  4. Write down two security threats to the service and measures against them.
Assessment criteria (individual assignment)
CriterionPoints
The architecture is correctly identified and justified with sources1
The structural diagram is clear, the data flows are shown1
Dcs and DP2P are calculated correctly (units MB → Mbit) and interpreted2
The security threats and measures are written with sound reasoning1

Summary

Client-serverThe server waits, the client asks; roles are strict, control is at the center.
SocketIP + port — the door of communication; the port distinguishes the service (443, 80, 53…).
Three tiersClient → application server → database; the client does not see the database.
P2PA peer both receives and gives; pure (DHT) and hybrid (tracker).
BitTorrentPieces, hash, rarest first, tit-for-tat.
ScaleDcs grows linearly with N, while DP2P is bounded.
SecurityClient-server — easy to manage, but a single target; P2P — uncontrolled, a trust problem.
ChoiceA single ledger and control → client-server; a large identical file for many → P2P; many apps — mixed.

Your lesson results

Your results are saved in this browser. Read all the sections and complete the exercises and assignments.

My personal notes