A basic HTTP server built from scratch in C with no external libraries. Part of my series building systems-level projects in C toward EduOS — a privacy-first, AI-native OS for African schools.
- TCP socket server listening on port 8080
- Parses raw HTTP GET requests
- Serves static HTML files from the working directory
- Returns proper HTTP headers including Content-Type and Content-Length
- Returns 404 for missing files
- Handles concurrent clients by forking a child process per connection
Creates a socket, binds it to port 8080, and listens for incoming connections. Each connection is accepted in a loop and handed to a child process via fork().
Reads the raw request bytes off the socket. Extracts the first line
(e.g. GET /index.html HTTP/1.1) and uses strtok to pull out the
requested file path.
Opens the requested file relative to the working directory. Sends a proper HTTP response with Content-Type and Content-Length headers, followed by the file contents in chunks. Returns a 404 response if the file doesn't exist.
After accept(), the server forks a child process to handle each client. The parent immediately loops back to accept the next connection. SIGCHLD is ignored so the OS auto-reaps finished children with no zombies.
mkdir build && cd build
cmake ..
make./xservPlace your HTML files in the working directory, then open:
http://localhost:8080/index.html
xserv/
├── main.c # Full server implementation
├── index.html # Sample static file
└── CMakeLists.txt
socket()— creates a network endpointbind()— assigns the socket to an address and portlisten()— marks the socket as passive, ready to acceptaccept()— blocks until a client connects, returns a new fdfork()— spawns a child process per client for concurrencyfseek/ftell— gets file size for Content-Length headerSO_REUSEADDR— allows immediate port reuse after restartSIGCHLD + SIG_IGN— auto-reaps child processes
This is Project 13 in my C systems programming series. Previous projects include a memory allocator, file explorer, process monitor, and terminal text editor — all built from scratch.