A lightweight multithreaded HTTP server built in C++ using POSIX sockets and a thread pool. The server handles multiple client connections concurrently, serves static files over HTTP, determines the correct MIME type for responses, and logs incoming requests.
- Multithreaded architecture using a fixed-size thread pool
- Concurrent handling of multiple client connections
- HTTP GET request parsing
- Static file serving from the
wwwdirectory - Automatic MIME type detection
- HTTP response generation (
200 OK,404 Not Found) - Request logging
- Cross-platform socket support (Linux and Windows)
- C++
- POSIX Sockets
- C++ Standard Library
- Multithreading (
std::thread) - Mutexes
- Condition Variables
- TCP/IP Networking
.
├── server.cpp # Server initialization and client connection handling
├── threadpool.cpp # Thread pool implementation
├── threadpool.h
├── utils.cpp # HTTP request parsing and response utilities
├── utils.h
├── www/ # Static website files
│ ├── index.html
│ ├── style.css
│ └── ...
└── README.md
- Initialize the server socket.
- Configure socket options (
SO_REUSEADDR). - Bind the socket to the specified port.
- Start listening for incoming client connections.
- Create a thread pool with worker threads.
- Accept incoming client connections.
- Add each client connection as a task to the thread pool.
- Worker threads process HTTP requests, serve files, and send responses.
- Close the client connection after the response is sent.
Client
│
▼
accept()
│
▼
Thread Pool
│
▼
handleClient()
│
▼
parseRequest()
│
▼
Requested File?
┌──────┴──────┐
│ │
No Yes
│ │
404 Read File
│ │
└──────┬──────┘
▼
Generate HTTP Response
│
▼
send()
│
▼
Close Connection
Currently supported:
GET
Unsupported methods return an error response.
200 OK404 Not Found
- HTML
- CSS
- JavaScript
- JSON
- TXT
- PNG
- JPG / JPEG
- GIF
- SVG
Unknown file types are sent as:
application/octet-stream
g++ -std=c++17 -pthread server.cpp threadpool.cpp utils.cpp -o serverg++ -std=c++17 server.cpp threadpool.cpp utils.cpp -lws2_32 -o server.exe./serverThe server starts listening on the configured port.
Open your browser and visit:
http://localhost:<PORT>
Replace <PORT> with the port configured in the source code.
- HTTP POST support
- Persistent connections (Keep-Alive)
- Directory listing
- File upload support
- HTTPS using SSL/TLS
- Configuration file support
- Thread pool resizing
- HTTP/1.1 compliance improvements
- HTTP caching
- Better error handling
This project helped strengthen understanding of:
- TCP/IP networking
- POSIX socket programming
- Client-server architecture
- HTTP protocol fundamentals
- Multithreading in C++
- Thread pools
- Mutexes and condition variables
- Synchronization
- Static file serving
- MIME type handling
- Resource management
- Concurrent server design
This project is intended for educational and learning purposes.