Multi-threaded TCP Chat Server
A chat server that accepts multiple simultaneous client connections over TCP sockets, broadcasting messages between clients using a thread-per-connection model with pthread mutexes protecting shared client state. Learners practice POSIX sockets, pthreads, and mutex-based concurrency control in C.
- Estimate
- ~12h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
pthread · sys/socket.h · netinet/in.h
Project roadmap
- 01
Set up the listening TCP socket
~1.5hCreate, bind, and listen on a TCP socket, accepting incoming connections in a loop.
- 02
Spawn a thread per client connection
~2hFor each accepted connection, spawn a pthread that handles reading messages from that client.
- 03
Implement thread-safe client registry
~2.5hMaintain a shared list of connected client sockets protected by a mutex, supporting add/remove.
- 04
Implement message broadcasting
~2.5hWhen a client sends a message, iterate the client registry under lock and write it to all other sockets.
- 05
Add nickname and join/leave notifications
~1.5hLet clients set a nickname on connect and broadcast join/leave/system messages.
- 06
Handle disconnects and cleanup gracefully
~2hDetect closed connections, remove them from the registry, and free resources without leaking or crashing.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack