Computer Network Tutorial: Understanding Client-Server vs Peer-to-Peer

beginner
13 min

Computer Network Tutorial: Understanding Client-Server vs Peer-to-Peer

Welcome to our in-depth guide on the fundamental concepts of computer networks! In this lesson, we'll be diving into Client-Server and Peer-to-Peer models, two essential strategies used in network communication.

What is a Computer Network? 🎯

A computer network is a collection of interconnected devices (computers, servers, smartphones, etc.) that can exchange data and resources. These networks can range from small home networks to massive global networks like the internet.

Client-Server Model 💡

In the Client-Server model, one device (the Client) requests resources or services from another device (the Server). This model is common in web applications, where your browser (Client) sends requests to a web server (Server) for web pages, images, and other resources.

Here's a simple example of a Client-Server model using HTTP (Hypertext Transfer Protocol) in Python:

python
# Server from http.server import BaseHTTPRequestHandler, HTTPServer class MyHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(b'Hello, World!') httpd = HTTPServer(('localhost', 8000), MyHTTPRequestHandler) httpd.serve_forever() # Client import socket client_socket = socket.socket() client_socket.connect(('localhost', 8000)) response = client_socket.recv(1024).decode() print(response)

In this example, the Server listens for requests on localhost port 8000 and sends the response "Hello, World!" to the Client when a request is made.

Peer-to-Peer Model 📝

In the Peer-to-Peer model, all connected devices (Peers) can act as both Clients and Servers, sharing resources directly with other devices. This model is common in file sharing networks and online gaming.

Here's a simple example of a Peer-to-Peer file sharing program using Python's socket module:

python
# Peer - Sender import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 8001)) server_socket.listen(1) client_socket, address = server_socket.accept() file_data = open('file.txt', 'rb') while True: data = file_data.read(1024) if not data: break client_socket.sendall(data) file_data.close() client_socket.close() # Peer - Receiver import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('localhost', 8001)) file_data = open('received_file.txt', 'wb') while True: data = client_socket.recv(1024) if not data: break file_data.write(data) file_data.close() client_socket.close()

In this example, one peer sends a file named 'file.txt' to another peer, and the other peer receives and saves it as 'received_file.txt'.

Quiz: Client-Server vs Peer-to-Peer

Quick Quiz
Question 1 of 1

What is the main difference between the Client-Server and Peer-to-Peer models?

By understanding the Client-Server and Peer-to-Peer models, you're well on your way to grasping the fundamentals of computer networks. Stay tuned for our next lesson, where we'll delve deeper into network protocols! 💡