Networking Basics

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-12-30

Problem Statement

Design and implement a simplified networking module that demonstrates fundamental networking concepts such as client-server communication, request-response handling, and data transmission over sockets.


The goal is to help you understand how network protocols, connections, and data flow operate at a low level, using object-oriented design principles.


📋 Problem Description


You are required to design a mini client-server system where:


•A Server listens for incoming client connections,
•A Client connects to the server, sends a request,
•The Server processes it and sends back a response.


This problem focuses on the LLD of networking concepts, not on real-world distributed scalability — but you should still think in terms of extensible design and clean abstraction.


🧱 Core Requirements


1. Classes to Design


•Server — Listens for incoming connections, handles requests.
•Client — Connects to the server and sends requests.
•Request — Represents data being sent from client to server.
•Response — Represents server’s reply to the client.
•Connection (optional) — Manages socket-like connection objects between clients and the server.


⚙️ Functional Requirements


1.Client Operations
•Establish a connection with the server.
•Send a request (e.g., a message or command).
•Receive and display the response from the server.
2.Server Operations
•Listen on a port for incoming client connections.
•Accept requests and log them.
•Generate appropriate responses (e.g., echo the message or perform a small computation).
•Handle multiple clients sequentially (or concurrently if extended).
3.Communication Protocol
•Use a simple text-based or JSON-based message format:
•Example: { "action": "echo", "message": "Hello Server" }
•Support request parsing and validation.
4.Error Handling
•Handle cases like connection timeout, invalid requests, or server unavailability.


🧩 Example Scenario


Client Side:

1 2client = Client("localhost", 8080) 3resp client.send_request("Hello, Server!") 4print(response) # Expected output: "Server received: Hello, Server!"

Server Side:

1 2server = Server(port=8080) 3server.start() 4 5# On receiving a message: 6# Input: "Hello, Server!" 7# Output: "Server received: Hello, Server!"


⚙️ Non-Functional Requirements


Aspect and Requirement


Scalability

Should support multiple concurrent clients (extendable).


Extensibility

Should support different message types (e.g., PING, ECHO, COMPUTE).


Reliability

Should handle dropped or malformed messages gracefully.


Security

Basic input validation to prevent invalid or malicious requests.


Performance

Low latency for message processing in simple local setup.


🧠 Design Discussion Points

•How would you abstract the socket handling logic from business logic?
•How would you extend the system to support multiple clients concurrently (threading or async)?
•Can you generalize this design to support HTTP-like request-response patterns?
•How would you introduce encryption or authentication in future versions?
•What design patterns (e.g., Observer, Factory, Singleton) could help here?

✅ Deliverables

•UML class diagram for Client, Server, Request, and Response.
•Implementation in Python or Java using object-oriented principles.
•Sample input/output demonstrating message exchange.
•Discussion of scalability extensions (multi-threaded or async server).

🚀 Stretch Goals (Optional Enhancements)

•Implement multi-threaded server handling multiple clients.
•Introduce basic authentication before accepting requests.
•Support broadcast messages (server to all clients).
•Add logging and metrics to track connection count and uptime.


Solution

Comments