A basic HTTP/1.1 server implementation written in C that handles multiple concurrent connections using poll-based I/O multiplexing.
This project implements a simple HTTP server from scratch in C, demonstrating low-level network programming and HTTP protocol handling. The server can handle basic GET and POST requests, serve files, echo messages, and parse HTTP headers.
- Berkeley Sockets API - Socket creation, binding, listening, and accepting connections
- TCP/IP - Understanding the transport layer protocol
- IPv4/IPv6 - Dual-stack socket addressing with
sockaddr_storage - poll() - Event-driven I/O multiplexing for handling multiple concurrent connections
- SO_REUSEADDR - Socket option to allow address reuse
- HTTP/1.1 - Request/response structure and status codes
- HTTP Methods - Handling GET and POST requests
- HTTP Headers - Parsing headers (User-Agent, Content-Length, Content-Type)
- MIME Types - Setting appropriate content types (text/plain, application/octet-stream)
- String manipulation -
strtok(),strstr(),sscanf(),snprintf() - Memory management - Dynamic allocation with
malloc(),realloc(),strdup(), andfree() - Signal handling - Graceful shutdown with SIGINT, SIGTERM, SIGHUP, SIGQUIT
- File I/O - Reading and writing files with
fopen(),fgets(),fprintf() - Command-line arguments - Parsing arguments for directory configuration
-
Route handling:
GET /- Returns 200 OKGET /echo/{message}- Echoes back the messageGET /user-agent- Returns the User-Agent header valueGET /files/{filename}- Serves files from a specified directoryPOST /files/{filename}- Creates files in the specified directory
-
Concurrent connections - Uses poll() to handle multiple clients simultaneously
-
Dynamic scaling - Doubles the client capacity when max connections are reached
-
Graceful shutdown - Handles termination signals to close sockets properly
# Compile
gcc -o server server.c
# Run without file serving
./server
# Run with file directory
./server --directory /path/to/filesThe server listens on port 4221 by default.
This project provided hands-on experience with:
- Low-level network programming and socket API
- HTTP protocol implementation from scratch
- Event-driven programming with poll()
- Manual memory management in C
- Parsing and handling text-based protocols
- Building a real-world server application