Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

CadvancedbackendAI generated

Multi-threaded TCP Chat Server

Build a chat server in C where many people connect at the same time over TCP (with a tool like nc or telnet), choose a nickname, and chat in a shared room. It uses the classic thread-per-connection design: each client gets its own POSIX thread, and a shared list of connected clients is protected by a mutex so threads never corrupt it. Messages are delivered to everyone else, joins and leaves are announced, and clients that disconnect suddenly are cleaned up without leaking memory or crashing the server. You'll learn POSIX sockets, pthreads, mutexes, avoiding deadlocks and race conditions, reading messages that arrive in pieces, and finding threading bugs with sanitizers.

Estimate
~13.5h
Steps
7
Completed by
0
Proposed by
codeseed.app

pthread · sys/socket.h · netinet/in.h

Project roadmap

  1. 01

    Listen for connections

    ~1.5h

    Create a Makefile-based C project compiled with -Wall -Wextra -pthread. Create a TCP socket, set SO_REUSEADDR, bind to a port given on the command line (default 9000) and listen. Accept connections in a loop, printing each client's address with inet_ntop. Ignore SIGPIPE so writing to a closed socket doesn't kill the server. Done when: the server starts, and nc localhost 9000 connects and is logged.

  2. 02

    Start a thread per client

    ~1.5h

    For each accepted connection, allocate a Client struct (socket, nickname, address) and start a detached pthread that handles it. For now, the thread echoes back what the client sends, then closes the socket and frees the struct when the client leaves. Check the return value of pthread_create. Done when: several nc clients can connect at once, and each gets its own echo.

  3. 03

    Keep a thread-safe client list

    ~2h

    Keep the connected clients in a fixed-size array (for example 100 slots) protected by a pthread_mutex_t, with add and remove functions that always lock and unlock correctly. When the server is full, send a message to the new client and close its connection. Done when: clients are added and removed correctly while many connect and disconnect.

  4. 04

    Read whole lines

    ~2h

    TCP delivers a stream of bytes, not separate messages, so give each client a buffer: append what recv returns, extract complete lines ending in \n, and keep the incomplete rest for the next read. Limit lines to 512 bytes and cut longer ones. Write a send_all function that keeps calling send until everything is written. Done when: messages split into pieces, or several arriving together, are handled correctly.

  5. 05

    Add nicknames and broadcasting

    ~2h

    Ask each new client for a nickname, rejecting empty or duplicate names. Broadcast each message as [nick] text to every other client. To avoid holding the lock while writing to slow sockets, copy the list of recipient sockets while locked, unlock, then send. Announce joins and leaves, and support /who and /quit. Done when: three clients can chat, and /who lists everyone.

  6. 06

    Handle disconnects and shut down cleanly

    ~2h

    Detect disconnects when recv returns 0 or an error, then remove the client, announce the departure, close the socket and free its memory, exactly once. Handle Ctrl+C with a signal handler that sets a flag, stop accepting new clients, tell connected clients the server is shutting down and close everything. Done when: killing clients abruptly and stopping the server leave no crash and no leaked memory.

  7. 07

    Find threading bugs and load test

    ~2.5h

    Build with -fsanitize=thread, run a script that connects 50 clients sending messages at the same time, and fix every data race reported. Then build with -fsanitize=address and check for memory errors and leaks. Describe the tests and results in TESTING.md. Done when: both sanitizers report no problems under load, and TESTING.md describes the tests.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~13.5h · 7 steps

Tech stack

pthreadsys/socket.hnetinet/in.h