NOTE: Currently I plan to add more features to this server. It currently lack proper file handling and much more
A simple C++ web server implementation for educational purposes. This project demonstrates basic socket programming, HTTP request handling, and file serving using C++ and POSIX APIs.
The server is currently hosted using Railway at https://cppwebserver-production.up.railway.app/
- Handles HTTP requests (GET)
- Serves static HTML and CSS files from the
assets/directory - Modular socket classes for easy extension
- Simple logging and connection management
hoothoot.cpp,hoothoot.hpp: Main place to use the Webserver classNetworking/Servers/: Server classes (e.g.,WebServer,SimpleServer)Networking/Sockets/: Socket abstraction classesassets/: Static HTML and CSS files to be servedCMakeLists.txt: Build configuration
- Make sure Cmake and C++ compiler is installed (I recommend using gcc with C++20)
<sys/socket.h>only works on Linux (and maybe MacOS, not 100% sure). Please run the project in Linux
Then run:
mkdir build && cd build
cmake ..
cmake --build .Or you can refer to Docker guideline to use docker
After building, run the server binary (e.g., cpp_web_server).
cd ../
./build/cpp_web_serverYou can use curl to send requests from the terminal:
curl http://127.0.0.1:4005/Replace PORT with the port your server is listening on. (By default it's 4005)
Or just open it in a web browser.
-
Using Makefile:
First create the image using
sudo make build
Then run using
sudo make run
-
If you don't have Make installed, then:
Create image using:
sudo docker build -t cpp_web_server .Create the container using the binding mount to
./assetssudo docker run -p 4005:4005 -v $(pwd)/assets:/app/assets cpp_web_server
We have 2 directories inside Networking, one is Socket for cpp sockets and other one is Servers for your servers. The project aim to provide a library like ability to create servers, not only for web but for your needs
SimpleSocket- Provides basic socket features, to createsocket()BindingSocket- Allows you tobind()to the networkListeningSocket- Allows you tolisten()to the socket.ConnectingSocket- [NOT RECOMMENDED TO USE] Purpose is toconnect()to socket, but currently not well maintained file
SimpleServer- This is what will mainly deal with the sockets inSocketsfolderWebServer- Our webserver, which defines how to handle http request and stuff
- If you cannot reuse a port after restarting, ensure the socket option
SO_REUSEADDRis set before binding. Or maybe try changing the port inhoothoot.cpp - If requests appear empty, check your socket read logic and buffer handling.
- For file serving issues, verify file paths and permissions.
Please check these resources:
- https://beej.us/guide/bgnet/ - Great guide recommended to read first
- https://www.youtube.com/watch?v=YwHErWJIh6Y
- Great tutorial but uses a bit old C++ version (like C-style casting and other stuff). This project gains the foundation from this video, but I've changed many things from project. Like:
- Using modern type casting
- Understanding the HTML and CSS request path, to give appropriate files
- Giving 404 error
- etc... (I plan to add more ^-^)
This project is for educational use.