A lightweight HTTP server built from scratch in C using raw POSIX sockets. No frameworks, no libraries — just syscalls and stubbornness.
- TCP socket server listening on port 8080
- Route table with function pointer dispatch
- Serves multiple pages: index, about, contact, and 404
- Persistent connection loop for handling multiple requests
- Clean error handling on socket, bind, and accept
gcc -o server main.c./serverThen visit http://localhost:8080 in your browser.
| Path | Handler | Description |
|---|---|---|
/ |
server_index_page |
Home page |
/about |
server_about_page |
About page |
/contact |
server_contact_page |
Contact page |
* |
server_404_page |
404 fallback |
The server follows the standard POSIX socket lifecycle:
socket()— create a TCP socketbind()— attach toINADDR_ANY:8080listen()— mark as passiveaccept()— block until a client connectsread()— pull the raw HTTP request- Route matching —
sscanfparses the method and path, then a loop through the route table finds the right handler write()— send back an HTTP responseclose()— clean up the client connection- Loop back to step 4
Do whatever you want with it.