Making Minimalist Web Server in C on Linux
In this video I will make a simple HTTP web server with the C Programming Language.
The code is not for production and only for fun 🙂
You can view the code I wrote in the video over here: https://gist.github.com/nir9/3d22d954a599a71c1ccf64ea63c4e38f
by Nir Lichtman
linux web server
For some reason modern web browsers do not handle header less HTTP responses nicely like wget does (which I used to demonstrate the web server in the video).
If you want to adapt the code in the video to work with modern web browsers the fix is simple, change the last argument of sendfile to 52 (length of index.html) and add the following into index.html file:
"HTTP/1.0 200 OK
Content-Type: text/html
Hello"
To make this dynamic to file size you can use the stat system call to get the file size before calling sendfile
love your approch
How did you get the man page for sockaddr_in? I have the man pages for socket and bind, but not the struct
So what you are telling me, is that I should read the documentation?
Very nice
so cool
This is just brilliant, in this short video there is so much to learn. <3 Thank you Sir for the amazing work you are doing for the people.
Should use htons() for the port and INADDR_ANY for bound address. You should also daemonise the process with a double fork so you can detach from the controlling TTY.
I was following along nicely and then line 24 (at 6:17) blew my mind wide open. Clever way to not assign a variable that will only be used once, never seen that syntax before.
Neat! How did you FIRST learn about the various C library functions you used?
Path traversal attack?
How were you able to do strchr(file, ' ') = 0? Shouldn't that throw a compiler error?
It's not really a web server when all the HTTP code is missing.
Socket tutorial fine, but it's not a web server.
is this linux or WSL? if wsl how did you get dwm like bar on windows. If its linux, what terminal is this, it looks like windows terminal
in bash/zsh, one can do straight at the prompt:
$ printf "%xn" 8080
to get the hex of 8080
this was a super video, I remember learning C from man pages back in 91 when I was put in charge of a new SunOS Sparc Network… this brought back many happy memories of doing a very similar client/server system for a tokenised command protocol… it was purely for my own brain r&d, but it taught me how useful man pages are, an art I feel is forgotten now just searching for an extant solution is the more lazy way.
Great tutorial.
just what I needed
In 10 minutes you taught me something my professor couldn’t teach our class in 3 weeks
Why you put the microphone inside your keyboard?
still – naming things is the hardest part 🙃
i like
libc/man just made a whole lot more sense to me.
2:36 little endian?
Didn’t watch the whole thing but you should really use better variable names then “f” and “s”
Thank you!
Good never imagined someone could do this in the age of express and django
please stop
It's pretty cool but would u actually use this type of thing?
Like if you just want to send html around wouldn't go or rust be a better choice?
If you are in neovim (idk about vim) you can use `Man` (capitalized) command to open manfiles in vim buffers. I hope this can be helpful.
How are you using something like i3 but also using the windows terminal?
It's a really interesting how modern programmers do coding. Old school programmers took sockets as the last step of programming, but the new generation do the opposite. Thank you for the video, it's really educational.
i like how you used manuals, never saw anyone else do that in a tutorial lol, and it never occured to me personaly to even try and use them