3 ms·
"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 assig
by st_goliath 4y ago
"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.
I reused the final version for a different university exercise instead of bothering with Tomcat as I was required, and back in 2015, dumped the end result here: https://github.com/AgentD/websrv https://github.com/AgentD/websrv where I kept adding to it for a while 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 exercises now 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.
- 0xfaded 4y agoI agree that writing the network code is more interesting. Perhaps make a really simple proxy server that accepts connections and forwards data to another http server
- mobilio 4y agoBeej is fantastic resource!
- shanebellone 4y agoThis sounds like a ton of fun. I would have enjoyed this educational path.
- st_goliath 4y agoCome to think of it, another surprisingly simple protocol you might want to have a shot at for the fun is VNC. It's also really rewarding to end up with a picture on the screen, once it works. Nowadays the entry barrier to real-time graphics programming is IMO really darn high for beginners, but a simplistic VNC server might get you a step closer to the MS-DOS days, where we could literally draw pixels into RAM. (Or, you could just extend your tiny HTTP server to support Motion JPEG, which is basically just a single HTTP response header, followed by a diarrhea of JPEG images.)
- lifewallet_dev 4y agoWhere did you go to school? And how come every college educated (mostly in the US) developer I've met has never done that before...
- st_goliath 4y agoAustria, HTL from age 14 to 19 (https://en.wikipedia.org/wiki/H%C3%B6here_Technische_Lehranstalt https://en.wikipedia.org/wiki/H%C3%B6here_Technische_Lehrans...). Wikipedia calls it college for some reason, our English teacher always called it a high school (with a focus on engineering disciplines; EE in my case) and compared it to such when showing us charts of education systems in UK, US vs Austria. I'm not sure how various schools/countries do ICT education in comparison, but I at least heard that friends of mine who stayed in grammar school past age 14 were exposed to Borland Delphi at some point. Every time the topic "tiny webserver in C" come up on HN, I always thought that the people who never did something like this were perhaps self-taught (similar to "write a tiny shell in C" which was also a standard exercise in our universities operating systems course). I have a hard time trying to imagine how one could possibly teach a college or university level course on networking without doing exercises like that.
- lloeki 4y agoAs students c.a 2004 we were tasked with writing a MTA. Actually, many MTAs. Divided in small teams (1-3 people), each MTA would ultimately be pointed to the next MTA, and the teacher would use telnet to send a message over SMTP to the first one, the goal being that the message had to reach the last one in the chain. Grades were attributed based on whether a team's implementation failed, crashed, mangled the message and whatnot, and also as a whole to promote team spirit all in good faith and sports. It was quite fun. (pretty confident we did HTTP things too although I have no actual recollection of it)