This is a simple, multi-threaded web server written in C. It's capable of serving static files and handling multiple simultaneous connections.
- Multi-threaded: Handles multiple clients concurrently using POSIX threads.
- Static File Serving: Serves static files (HTML, CSS, JavaScript, images, etc.).
- Configuration: Uses a
config.jsonfile for easy configuration of port, backlog, and root directory. - MIME Type Support: Correctly identifies and serves common file types with the appropriate MIME types.
- Error Handling: Sends appropriate HTTP error codes (400, 404, 500) to the client.
- cJSON: Used for parsing the
config.jsonfile. The source code for cJSON is included in thecjsondirectory.
-
Compile:
gcc -o server.out server.c server_utils.c cjson/cJSON.c -lpthread
-
Configure: Create a
config.jsonfile in the same directory with the following format:{ "port": "8080", "backlog": 10, "root": "./public" }port: The port number for the server to listen on.backlog: The maximum number of pending connections.root: The root directory from which to serve files.
-
Run:
./server.out
The server will then be running and listening for connections on the specified port.
server.c: The main entry point of the application. It sets up the socket, listens for incoming connections, and creates a new thread to handle each client.server_utils.h: Header file for the server utility functions.server_utils.c: Contains helper functions for the server, including:- Socket setup and configuration.
- Request parsing.
- Response generation.
- File handling and MIME type detection.
cjson/: Contains the cJSON library for parsing JSON.public/: The default root directory for serving static files.config.json: Configuration file for the server.