Project 3: Multi-Client Chat Server (select/poll)

A chat server that handles multiple simultaneous clients using select() or poll(), broadcasting messages from any client to all others. Clients can set nicknames and see who’s online.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages C, Rust, Go
Difficulty Level 2: Intermediate
Time Estimate 1 week
Knowledge Area I/O Multiplexing, Event-Driven Programming
Tooling IRC-like chat server
Prerequisites Project 2, comfort with C++ containers (vector, map)

What You Will Build

A chat server that handles multiple simultaneous clients using select() or poll(), broadcasting messages from any client to all others. Clients can set nicknames and see who’s online.

Why It Matters

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

Core Challenges

  • Managing multiple file descriptors → maps to fd_set for select, pollfd array for poll
  • Handling the accept socket alongside client sockets → maps to mixing listening and connected sockets
  • Broadcasting to all clients → maps to tracking connected clients, handling partial sends
  • Detecting disconnects in a multiplexed loop → maps to read() returning 0, poll POLLHUP

Key Concepts

  • select() System Call: “The Linux Programming Interface” Section 63.2 - Kerrisk
  • poll() vs select(): “UNIX Network Programming, Volume 1” Section 6.3 - Stevens
  • Event Loop Pattern: “Linux System Programming” Chapter 2 - Robert Love
  • fd_set Limitations: 1024 fd limit on many systems

Real-World Outcome

# Terminal 1: Start server
$ ./chat_server 9000
Chat server started on port 9000
[12:00:01] Client connected from 127.0.0.1:52431 (fd=4)
[12:00:05] Client connected from 127.0.0.1:52432 (fd=5)
[12:00:10] <Alice> Hello everyone!
[12:00:12] <Bob> Hey Alice!
[12:00:30] Client disconnected (fd=4)

# Terminal 2: Client 1
$ ./chat_client localhost 9000
Connected! Enter /nick <name> to set nickname.
/nick Alice
You are now known as Alice
Hello everyone!
<Bob> Hey Alice!

# Terminal 3: Client 2
$ ./chat_client localhost 9000
/nick Bob
<Alice> Hello everyone!
Hey Alice!

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_CPP_NETWORK_PROGRAMMING.md
  • “UNIX Network Programming, Volume 1” by W. Richard Stevens