Course map

Topic 010 / Core

Client–server architecture

Chapter 1.4 · Client–Server Architecture

What and why

Almost everything in this book is some variation of one machine (the client) asking another (the server) to do something and waiting for an answer. This topic fixes that model at its lowest practical level — sockets, not frameworks — so every later protocol (Part 3) is understood as a layer built on top of it, not as magic. Builds on: → 9 (CPU, RAM, storage and I/O — the network hop this topic now puts a shape around).

Diagram

CLIENT                                   SERVER
  |                                        | listen() on a port
  |--- connect (TCP handshake, → 25) ----->| accept() -> new socket for this client
  |--- send request bytes ----------------->| read() the bytes
  |                                         | parse into a request
  |                                         | handle it (application logic)
  |<-- send response bytes -----------------| write() the response
  |--- close, or keep-alive for reuse ----->|

How it works

  1. The server calls listen() on a bound address and port, then waits for incoming connections.
  2. The client calls connect(); the OS performs the TCP handshake (→ 25) before either side sends application data.
  3. The server's accept() returns a new socket dedicated to that one client; the original listening socket stays free to accept others.
  4. The server reads raw bytes off the socket and parses them according to whatever protocol is layered on top — HTTP and friends are Part 3's job, not this topic's.
  5. The server writes the response back on the same socket, then closes it or keeps it open for reuse.
  6. How many clients one server instance can serve at once depends entirely on its process/thread model (→ 5, → 6) — a server blocked on one client's slow read can't accept a new connection until it's free.

Practical example

A minimal server, traced end to end: bind and listen on a port; loop forever calling accept(), which blocks until a client connects; read the first line of bytes sent; if it starts with GET , write back a fixed 200 OK response with a short body; otherwise write back 400 Bad Request. Every byte that crosses the wire in this trace is produced by one of those four steps — nothing else is involved yet.

When to use it

  • Use this raw model to understand what any higher-level framework (Part 3) is doing underneath — the mental model doesn't change, only the amount of work done for you does.
  • Avoid building anything production-facing from raw sockets — a proven HTTP server or framework already handles the edge cases a minimal exercise ignores: partial reads, pipelined requests, keep-alive, malformed input.
  • Failure scenarios: assuming one read() call returns exactly one full request — TCP makes no such guarantee, a request can arrive in pieces; leaking sockets by never closing them, eventually exhausting file descriptors; blocking accept()/read() on a single thread, which serializes every client even when nothing else is wrong (→ 6).
  • Simpler alternative: for anything beyond this exercise, use an existing HTTP library — reimplementing an HTTP server is a solved problem; the value here is in the mental model, not in shipping the result.

Practice

(mechanism — build a tiny version; 20–30 min, no lab services, any language with raw sockets).

  1. Build a minimal TCP server that accepts one connection, reads the request line, and returns a fixed HTTP response.
  2. Confirm it from a real client (a browser or curl), and separately inspect the raw bytes with a second tool (e.g., netcat).
  3. Connect a second client while the first is still open; confirm the second one waits until the first is handled — the direct, visible reason the thread/connection-handling model (→ 6) matters.

Done when the browser or curl receives a correct response, and you've shown the second client has to wait for the first to finish.

Key takeaway

A server is listen → accept → read → handle → write, repeated per client; everything in Part 3 is a protocol built on top of exactly this loop. Done when you can: trace a request through these five steps and explain why one slow client can block another under a naive handling model. Used next in → 23 (OSI and TCP/IP models), → 28 (HTTP fundamentals) — both Part 3.

References

None — this topic makes no external claims.

Source directory

Search the guide