Project 3: A Multi-Threaded TCP Web Server

A simple multi-threaded TCP server that listens for connections and serves a static HTML file. You will implement a thread pool to limit the number of concurrent connections.

Quick Reference

Attribute Value
Primary Language Rust
Alternative Languages C (with pthreads), Go
Difficulty Level 3: Advanced
Time Estimate 1-2 weeks
Knowledge Area Concurrency / Networking
Tooling std::net, std::thread
Prerequisites Project 1, understanding of basic HTTP and TCP concepts.

What You Will Build

A simple multi-threaded TCP server that listens for connections and serves a static HTML file. You will implement a thread pool to limit the number of concurrent connections.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Listening for TCP connections → maps to using std::net::TcpListener
  • Spawning threads to handle connections → maps to using std::thread::spawn and closures with move
  • Building a thread pool → maps to sharing a queue of jobs between worker threads
  • Safely sharing the job queue → maps to the Arc<Mutex<T>> pattern for shared, mutable state

Key Concepts

  • Concurrency vs. Parallelism: “The Rust Programming Language” Ch. 16
  • Threads: “The Rust Programming Language” Ch. 16
  • Shared-State Concurrency (Arc, Mutex): “The Rust Programming Language” Ch. 16
  • TCP Sockets: “The Linux Programming Interface” by Michael Kerrisk, Ch. 56

Real-World Outcome

$ cargo run
   Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/web-server`
Server listening on 127.0.0.1:7878

# Open http://127.0.0.1:7878 in your browser and see your HTML page.
# Open multiple tabs to see the multi-threading in action.

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_RUST_FROM_FIRST_PRINCIPLES.md
  • “The Rust Programming Language” Ch. 20