Today I rebuilt and earlier attempt at a HTTP web server from scratch using C and Berkeley sockets! Hopefully it now better represents better quality low-level network programming.
When you run this server, you'll get a fully functional web server with:
- Cyberpunk-themed web interface with neon colors and gradients
- Multiple interactive pages - main server info, source code viewer, and 404 error demo
- Real HTTP request/response handling - your browser actually talks to the C program!
- Berkeley sockets implementation - the same networking foundation used by real web servers
The coolest part is watching the terminal output as your browser makes requests - you can see the actual HTTP parsing happening in real-time!
cd c/http_web_server
# Build the server
make
# Start the server (runs on port 53557)
./server
# Visit in your browser
http://127.0.0.1:53557The server will show you live connection logs in the terminal while serving the web interface!
This implements the complete Berkeley sockets workflow that real web servers use:
- Create Socket -
socket(AF_INET, SOCK_STREAM, 0)creates the network endpoint - Bind to Port -
bind()attaches the socket to port 53557 - Listen for Connections -
listen()starts accepting incoming requests - Accept Clients -
accept()handles each browser connection - Parse HTTP Requests - Extract the URL path from browser requests
- Send HTML Responses - Return the cyberpunk-styled web pages
- Clean Up - Close connections and handle graceful shutdown
The server handles multiple routes: / (main page), /code (source viewer), and any other path triggers a 404 error page.
- Port number (line 10): Change
#define PORT 53557to any port you want - Buffer size (line 11): Adjust
BUFFER_SIZEfor larger/smaller requests - Cyberpunk colors (lines 30-60): Modify the neon color scheme in the CSS
- Add new routes (lines 578-597): Add more pages by extending the
send_response()function - Server title (line 196): Change "HTTP WEB SERVER ONLINE" to your own branding
- Code content (lines 369-553): Update the source code viewer with different functions
Building this web server was a huge leap in my C programming journey! I learned:
- Berkeley Sockets API - The foundation of all internet communication, from basic
socket()calls to handling multiple connections - HTTP Protocol - How browsers actually communicate with servers, parsing request headers and sending proper responses
- Network Programming - Understanding TCP/IP, port binding, and client-server architecture
- Memory Management - Handling dynamic buffer allocation for HTTP requests and responses
- Signal Handling - Graceful server shutdown with
SIGINTand proper resource cleanup - Web Development - Creating HTML/CSS interfaces that work with C backend systems
One of the most exciting moments is when you first see the browser successfully connect to the C program - it feels like building the internet from scratch! This is exactly how real web servers work under the hood.
Architecture:
- Single-threaded server handling one connection at a time
- Dynamic HTML content generation with embedded CSS styling
- Proper HTTP response headers and status codes
- Signal handling for clean shutdown with Ctrl+C
Key Functions:
main()- Sets up the socket, binds to port, and runs the connection loophandle_client()- Processes individual browser connectionsparse_request()- Extracts URL paths from HTTP requestssend_response()- Routes requests to appropriate HTML pageshandle_signal()- Graceful shutdown when interrupted
Network Concepts:
- TCP/IP socket programming with
AF_INETandSOCK_STREAM - Network byte order conversion with
htons() - HTTP request parsing and response generation
- Connection lifecycle management
This project represents my transition from basic C programming to building real networked applications - the foundation for understanding how the internet actually works!