Live data from Hacker News

Ask HN: Resources for Building a Webserver in C?

news.ycombinator.com

41–50 of 67 posts

Re: Ask HN: Resources for Building a Webserver in C?

#41

"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 where I kept adding to it for a while for fun. The H…

Come 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.)

Re: Ask HN: Resources for Building a Webserver in C?

#42

Earlier quoted context omitted.

conceptually things are pretty limited - this is really simple if you are just playing around, and are just doing http 1 o bind a socket o manage incoming connections o parse http headers o 'routing' - demultiplex handlers based on URL o encode response unless its a little embedded thing one usually uses some kind of select/epoll machinery to multiplex the work on each channel. I guess thread-per-request is another m…

A little context: I'm specifically interested in compressing my analytics stack. I'm currently interacting with HTTP Messages at a higher level. The problem, as I see it, is the webserver is already doing this work but without useful output. I'd like to implement my analytics paradigm natively at the webserver level.

In that case you might be better off looking at writing a plugin for your existing webserver.

nginx, apache, etc, all allow writing modules that can be invoked at various parts of the HTTP-communication.

Re: Ask HN: Resources for Building a Webserver in C?

#43
post #39

> I'd also be interested in opinionated writeups/positions. Ok then! Writing a web server in C is bloody stupid. A web server's performance is mostly limited by concurrency issues and various IO latencies, not by how fast the machine code runs. Thus, there are zero advantages to writing a web server in C and mountains of disadvantages. Save your time and write your web server in a good language, Java, Go, Python, Nim…

I've built one with Python along with a custom WSGI app.

Also, Apache and NGINX say hello.

Re: Ask HN: Resources for Building a Webserver in C?

#44
post #19

Earlier quoted context omitted.

Being largely unfamiliar with C, I would prefer conceptual resources over source code. The why is more important than the how. Hopefully that makes sense?

C is the native language of Unix, so in a way it really is the language for formally discussing Unix concepts.

I'm surprised this isn't the popular opinion.

Re: Ask HN: Resources for Building a Webserver in C?

#45

"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 where I kept adding to it for a while for fun. The H…

Where did you go to school? And how come every college educated (mostly in the US) developer I've met has never done that before...

Re: Ask HN: Resources for Building a Webserver in C?

#46
Here's a 2000 line web server I wrote in C (with a focus on readability) which runs https://ipv4.games/ You can read the code at https://github.com/jart/cosmopolitan/blob/master/net/turfwar... This web server runs on a single VM. It was handling about 300 write 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. This service is frequently targeted by hackers so you can learn a lot about things like DDOS protection by reading the source.

Re: Ask HN: Resources for Building a Webserver in C?

#47

"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 where I kept adding to it for a while for fun. The H…

Where did you go to school? And how come every college educated (mostly in the US) developer I've met has never done that before...

Austria, HTL from age 14 to 19 (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.

Re: Ask HN: Resources for Building a Webserver in C?

#48

Complete OpenBSD + Sqlite + C + httpd toolkit for building websites. http://bsd.lv/ I recommend swapping out C for a safer&more productive language.

Is there a safe and productive language that produces binaries that run fast? Between the rising energy prices and CO2 pollution it would be very irresponsible to use languages like Python or Ruby.

Re: Ask HN: Resources for Building a Webserver in C?

#49
post #48

Complete OpenBSD + Sqlite + C + httpd toolkit for building websites. http://bsd.lv/ I recommend swapping out C for a safer&more productive language.

Is there a safe and productive language that produces binaries that run fast? Between the rising energy prices and CO2 pollution it would be very irresponsible to use languages like Python or Ruby.

Yes, Rust.

Re: Ask HN: Resources for Building a Webserver in C?

#50
post #49
post #48

Earlier quoted context omitted.

Is there a safe and productive language that produces binaries that run fast? Between the rising energy prices and CO2 pollution it would be very irresponsible to use languages like Python or Ruby.

Yes, Rust.

You're right. I actually managed to skip over "small" in front of "binaries", so that's my mistake, but for fat binaries with decent performance Rust is a great language, as long as you don't care about glacial development speed.
Post reply on HN