Multi-client TCP chat server in C, built on select() — no threads, no libraries.
A small, self-contained demo written in pure C — just the standard library and POSIX sockets. Part of the Corg-Labs collection of single-file C programs.
- One listening socket accepts many clients on a single port
- A single
select()loop multiplexes the listener and every client — one thread total - Anything one client sends is relayed (broadcast) to all the others
- A live dashboard redraws on each event, showing every client's name, IP, port and message count
gcc relay.c -o relay
./relay 8080
Then connect from one or more other terminals:
nc localhost 8080
Once connected, just type to chat. Special commands:
/nick NAME— change your display name
Every line you send shows up as [yourname] message in everyone else's terminal,
and join/leave notices are broadcast automatically.
- Handles up to 32 simultaneous clients (raise
MAXto allow more) SO_REUSEADDRlets you restart the server immediately without "address in use"- Blank lines are ignored so stray newlines don't spam the room
- Press Ctrl-C to stop the server