Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multithreaded HTTP Server

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.

Features

  • Multithreaded architecture using a fixed-size thread pool
  • Concurrent handling of multiple client connections
  • HTTP GET request parsing
  • Static file serving from the www directory
  • Automatic MIME type detection
  • HTTP response generation (200 OK, 404 Not Found)
  • Request logging
  • Cross-platform socket support (Linux and Windows)

Technologies Used

  • C++
  • POSIX Sockets
  • C++ Standard Library
  • Multithreading (std::thread)
  • Mutexes
  • Condition Variables
  • TCP/IP Networking

Project Structure

.
├── 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

How It Works

  1. Initialize the server socket.
  2. Configure socket options (SO_REUSEADDR).
  3. Bind the socket to the specified port.
  4. Start listening for incoming client connections.
  5. Create a thread pool with worker threads.
  6. Accept incoming client connections.
  7. Add each client connection as a task to the thread pool.
  8. Worker threads process HTTP requests, serve files, and send responses.
  9. Close the client connection after the response is sent.

HTTP Request Flow

Client
   │
   ▼
accept()
   │
   ▼
Thread Pool
   │
   ▼
handleClient()
   │
   ▼
parseRequest()
   │
   ▼
Requested File?
 ┌──────┴──────┐
 │             │
No            Yes
 │             │
404        Read File
 │             │
 └──────┬──────┘
        ▼
Generate HTTP Response
        │
        ▼
send()
        │
        ▼
Close Connection

Supported HTTP Methods

Currently supported:

  • GET

Unsupported methods return an error response.

Response Types

  • 200 OK
  • 404 Not Found

Supported File Types

  • HTML
  • CSS
  • JavaScript
  • JSON
  • TXT
  • PNG
  • JPG / JPEG
  • GIF
  • SVG

Unknown file types are sent as:

application/octet-stream

Build

Linux

g++ -std=c++17 -pthread server.cpp threadpool.cpp utils.cpp -o server

Windows (MinGW)

g++ -std=c++17 server.cpp threadpool.cpp utils.cpp -lws2_32 -o server.exe

Run

./server

The 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.

Future Improvements

  • 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

Learning Outcomes

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

License

This project is intended for educational and learning purposes.

About

A multithreaded HTTP server built in C++ using POSIX sockets and a thread pool. Supports HTTP GET requests, static file serving, MIME type detection, request logging, and concurrent client handling on Linux

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages