Skip to content

Peu77/mini_serv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini_serv (examrank06)

A small TCP chat server (42 mini_serv-style) implemented in C.

It accepts multiple clients on 127.0.0.1:<port>, assigns each a numeric id, and broadcasts:

  • arrivals: server: client <id> just arrived\n
  • departures: server: client <id> just left\n
  • messages (line-based): client <id>: <line>\n to all other clients

Messages are delimited by \n. If a client sends multiple lines, each line is broadcast separately with the prefix. Partial TCP frames are handled by buffering until a newline is received.

Note: serv.c in this repo is a “clean/testing-friendly” version and uses a few exam-forbidden functions (notably signal() and C11 atomics) so you can stop the server with Ctrl+C without leaking sockets during testing.

Files

  • serv.c — the server implementation
  • makefile — build rules (+ helpers for Docker)
  • dockerfile — Debian image with cc, valgrind, netcat and bun
  • tester/tester.ts — Bun-based functional + stress tester

Build

make

This produces ./serv.

Run

The server expects a single argument: the port.

./serv 8080

The server binds to 127.0.0.1 only.

Connect manually (netcat)

In separate terminals:

nc 127.0.0.1 8080

Type lines and press Enter. Other connected clients will receive your messages prefixed as client <id>: ....

Run the Bun tester

The tester targets 127.0.0.1:8080 by default.

  1. Start the server:
./serv 8080
  1. In another terminal, run the tester from the tester/ folder:
bun tester.ts

What it checks (high-level):

  • arrival/departure broadcasts
  • sender does not receive its own messages
  • line-by-line prefixing for multi-line sends
  • TCP fragmentation handling (split writes)
  • stress: connects ~1000 clients (bounded concurrency) and sends a ~100kB line in <=800B chunks

Docker workflow

The provided dockerfile creates a Debian image with build tools, valgrind, nc, and bun.

Build the image:

make docker_build

Run an interactive shell inside the container:

make docker_run

Run a second shell inside the container:

make docker_attach

Inside the container:

make
./serv 8080

Then (in another container shell, or background the server), you can run:

cd tester
bun tester.ts

Implementation notes / limitations

  • Line buffering: each client accumulates bytes until \n, then broadcasts that line.
  • Message size: there is a large fixed buffer (MAX_MESSAGE_LENGTH) used for assembling a line. Extremely long lines without newlines can overflow if they exceed this limit.
  • The server uses select() and fd_sets and stores per-client state indexed by file descriptor.
  • Broadcast uses send(..., MSG_NOSIGNAL) to avoid SIGPIPE when writing to closed sockets.

Troubleshooting

  • Tester says it can’t connect: ensure the server is running on port 8080 and bound to 127.0.0.1.
  • Port already in use: stop any existing server or pick another port (and update tester/tester.ts if needed).
  • Docker make target typo: the Makefile includes docker_build; running containers is done with the docker run ... command shown above.

About

A little tcp chat server written in c

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors