Our great sponsors
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
-
src
Read-only git conversion of OpenBSD's official CVS src repository. Pull requests not accepted - send diffs to the tech@ mailing list.
-
epoll-server
C code for multithreaded multiplexing client socket connections across multiple threads (so its X connections per thread) uses epoll
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
"Write a tiny web server in C" was a standard exercise when I was in high school, and later again in university. I think I did that at least 3 times as an assignment so far.
Back in 2015, I dumped the result of the University exercise here: https://github.com/AgentD/websrv and kept adding to it for fun.
The HTTP 1.x protocol itself is pretty dead simple (if you ignore chunked requests/responses and such) and the more interesting part was actually the socket code.
Back in school, our teacher had us write a simple forking server and consult the corresponding man pages (man 2 socket, man 7 socket, man 7 tcp). It was an acceptable minimal solution to not even look at the client request, respond with a static string containing a dummy response header and HTML page, which absolutely works. Bonus points for parsing the request path and sending a file back, more extra points if the path was a directory and your server sends back an "index.html" file, further points if it instead generates an HTML directory listing on the fly.
When I visited our former teacher a couple years ago, he had modified the exercise somewhat. He brought a Beagle Bone Black with him to class, with a bunch of I2C sensors attached to it, and the extended exercise revolve around reporting temperature/humidity/... to the browser.
So, for "resource" I recommend the HTTP example on Wikipedia, as well as "Beej's guide to network programming" (as have others), as well as the man pages for quick reference.
You might find my epollserver interesting.
It multiplexes multiple clients (sockets) over a thread, so you can write an event loop in each thread and serve far more requests per thread than you could if it was one thread per client or one process per client.
Here's a 2000 line web server written in C that runs https://ipv4.games/ which you can read at https://github.com/jart/cosmopolitan/blob/master/net/turfwar... This web server runs on a single VM. It was handling about 300 requests per second earlier today. You can monitor its metrics here: https://ipv4.games/statusz It uses *NSYNC as its multi-threaded locking primitives, which was designed by the guy who created Google's lock server.
I found pulling this code apart a great learning experience.