77 comments

[ 4.3 ms ] story [ 122 ms ] thread
It's released in Nov,2020 but yes it's one of the best.
Finally mentions poll() and not only select() !
IIRC it did that 10 years ago as well, back when I read it for an intro class to C, OS principles and networking.
Do we need to know C to use this?
It uses the gcc compiler so C, or C++ can be applicable.

That being said, this book teaches network programming from the ground up so I would recommend understanding how to compile and output C code, then go through the book.

If you run into keywords, or syntax that you are not familiar with, just google it, and then keep working through the examples.

Mind you, the key takeaways from this book are not how to network practice IN C, but general practices. This knowledge can be applied to plenty of other languages such as Rust.

Not necessarily but it does help. These tutorials focus mainly on the Berkeley Sockets API which can be exposed as-is (or with slight modification) to many languages beyond just C/C++.
It is useful mainly if you have to do some netprog in C, and secondarily if you have to deal with language bindings that do a 1:1 mapping to the BSD socket API.

In other words, you will know when you need it - unless you forget it exists (note: bookmarking it might be a good idea, cause you could remember something like this exists but "what was the name again?").

You can learn C at the same time.
Besides the classic C book by K&R, what are some more modern C books that the HN community vouch for?
My favorite C programming book was Expert C Programming by Peter van der Linden, mainly for the method of reading any C declaration and for teaching me the difference between libc calls vs syscalls. There's also a book called Modern C by Jens Gustedt from 2020 that I have and is more beginner friendly.

The way I learned C is I printed out the Beej's guide client/server code and walked around staring at it until it made sense. (That and compiling/running it and looking up functions in the man pages.)

Expert C Programming is brilliantly written. I recently bought another copy because I lost mine; I don’t actually need it, but it’s a fun read regardless.
I did write a guide to C that's currently in beta. If it's any good or not... Well, I'll leave that up to you. :)

I also really like K&R2 and Van der Linden.

https://beej.us/guide/bgc/

I'll most definitely be checking out your guide! Thank you so much for your contributions in the community :) Will the latest revision of your Networking Guide be available on print any time soon? I'd love to buy a copy.
I think C Programming: A Modern Approach by K.N. King is great.
Has anyone actually even tried the K&R book in the past couple of decades and not run into issues like the assumption of unbuffered shell output? You get like one or two chapters in and things are already behaving outside of how they did when that book was written in the 1980s. It's a classic, yes, but I don't recommend it to anyone outside of people writing code in C89 for vintage systems... Yet the amount of suggestions I see from people who haven't actually learned C from scratch since the 1990s to read that particular title is high enough that I can't do anything but scratch my head over it. Tons has changed over thirty plus years.

If you're looking for something up to date I'd recommend this:

https://gustedt.gitlabpages.inria.fr/modern-c/

K&R2 is hard to beat for size and clarity of writing.

Could you elaborate on the "unbuffered shell output" comment?

I'm guessing he's talking about the putchar/getchar example in the first or second chapter?
Yeah that. It's extremely confusing and where a ton of people stop reading it.
I taught myself C from K&R 2, and a platform-specific (Amiga!) book, back when I was a teenager. In three decades, not much has really changed with the core language.
While the core language has stayed the same the tooling and development environment has changed quite a lot. Proper modern compiler flags (-Wall etc) for example would be a huge help to new learners and for best practices, but k&r isn't mentioning these. I still think it's a good book but if you throw it at someone new to this they're going to be wondering why what they are writing isn't behaving as expected.
No, not necessarily. You can use these APIs directly from many different languages. For example, Python has 'socket'[1], Ruby has 'socket'[2], Julia has 'Sockets'[3], PHP has 'socket_*'[4].

1. https://docs.python.org/3/library/socket.html

2. https://ruby-doc.org/stdlib-2.5.3/libdoc/socket/rdoc/Socket....

3. https://docs.julialang.org/en/v1/stdlib/Sockets/

4. https://www.php.net/manual/en/ref.sockets.php

The weirdest thing just happened. I was on https://beej.us/guide/ and I removed the last part of the URL to make it only https://beej.us and I pressed enter. This took me to cnet.com. I have never been on cnet.com before. I have no idea how this happened. Does anyone have a clue? I'm using Firefox if that matters. Did a web server somewhere get hit by a cosmic ray or is beej's server infected by something? This is super weird. When I try to do it again it just takes me to beej's site as expected.
Hm, unable to reproduce. Perhaps you have some weird browser extension installed or something intercepting DNS requests?
I couldn't reproduce, but I note the background image changes on reloads. Perhaps beej is hotlinking, and one of the images is based on cnet, who redirected to themselves?
Maybe CNET is offering me large sums of money to randomly redirect every hundredth visitor!

Actually, they're not, so this is weird. I can't repro, and ripgrep tells me "cnet" doesn't appear in my web pages anywhere.

If you find it, I'd love to know what it is.

Indeed, this guide is very informative and easy to digest. It's practically required reading for anyone taking CS 6200: Introduction to Operating Systems [1] in Georgia Tech's Online Master of Science Computer Science program.

Thank you, Beej. Looking forward to reading this again today.

1. https://omscs.gatech.edu/cs-6200-introduction-operating-syst...

Truly an instrumental resource for getting through Operating Systems course at Georgia Tech.
I've yet to break into this one (I've seen it recommended so many times on HN). I'm no network engineer, but networking is a lot of fun for me and I enjoy learning about how it all works. High Performance Browser Networking[0] was one of my favorite books on this topic. Very approachable, even to complete beginner. I suspect that Beej's guide is a bit more low level and I look forward to that!

[0]https://hpbn.co/

I think HPBN and Beej's guide are perfect companions to each other. They've been the most useful resources on my journey to learn more about networking.
Can you link to "HPBN"? I'm not familiar with what this initialism..
Hey all!

I did update it for IPv6, but that was several years ago.

So I'm not sure which update the OP is referring to.

Appreciate the kind words, though!

Thanks for your work on this. I read it when I was much younger and it hammered home how to treat stream sockets correctly.
Your tutorial demystified network programming for me when I was just a kid. It deserves all the praise it gets. Thank you!
I work in networking, probably because of 1. Your guide and 2. My professor. Thanks
Thanks for your hard work! When I discovered your guides ages ago I already had digested a good part of the famous text by W. Richard Stevens, but being my copy a very poorly translated edition (I did read English back then, but the imported book would cost a small fortune), I later had to check some things on your guides for consistency, so they turned out very useful anyway.
Thank for for writing and maintaining this for so long. As I wrote yesterday, it is my single favorite piece of programming literature in existence.
Hey! I've learned network programming with your guide when I was teenager - thank you!
Thank you so much for this guide!

When I was a kid, socket programming felt like a huge thing to me, like unlocking the next level of programming. "Imagine if I could get 2 separate computers to talk to each other!" But it felt impenetrable, and used lots of weird and confusing C tricks.

Your guide demystified the whole subject and put it in plain language that teenage me could understand, and with it, I was able to build my first network-based "hello world". It was a magical feeling to see those messages delivered across the network. Thanks Beej!

>> socket programming felt like a huge thing to me, like unlocking the next level of programming.

This was my story as well, I wonder how many people have been through this just like us!?

Same story here - 1998, teenager, unable to understand socket programming until I came across Beej’s guide at CSU Chico. Thank you Beej!
Same exact story - I was perhaps 9 or 10 and got to Beej's work on sockets. It was one of the more accessible things available to me (or known to me). I remember reading Stevens books next.

So interesting that so many people have the same experience

Seriously though, posix sockets and the C interface for them were designed to be as confusing as possible to someone new, right?
Hey! Thank you so much for your networking guide! It's my go to reference for socket programming and it is the documentation that I pointed my students to when I was giving a networking course :).

Not directly related but while you're here: you should _really_ add Marcus Rediker's Villains of all nations to your Pirates reading list. I highly recommend it! It's the best pirates' book ever =).

Thank you for writing this guide. I really enjoyed reading it and it helped me get my first job.
Your guides were legendary when I was just starting out. Thanks for that!
The IPv6 update really helped me at work, thank you!!
Wow unreal… I used your guide years ago for my first project when I was learning C. It was an IRC bot.

Thank you!

Thanks Beej been using your guides for a long time. Appreciate your hard work!
I just pulled it up to refresh my knowledge for a project and it was different. The thing that sticks out the most being different is that the code boxes are highlighted. My first time reading, it was standard grey boxes similar to some other guide (IPC guide I just read was still grey boxes!). Great work! It's the best reference!
Any plans to add support for the new async and zero copy APIs?
Mr. @dang, I would like to kindly request that the title is updated.
Correct. The title was editorilized. And not enough with that, it's not even true that the guide has been updated, not for years according to the author himself in the currently top-voted comment.

So in a way this ended up to be perfect clickbait. But nobody seems to complain because despite not having been updated the guide seems to recall many fond memories by the commenters.

Highlighting users does not work on HN, I think the recommendation is to send an email.

It's karma farming.

Summoning dang usually works, for some reason.

Beej: thanks a million ! I learnt all about sockets and select() from your guide back at uni over 20 years ago. I was trying to make an autonomous model helicopter for my 3rd year project, fuel oil powered, with a scarily large rotor diameter… so I made this single threaded emergency stop daemon that listened to a few sockets, including the big red button, which would interrupt the test joystick data and write out some engine-off and neutral collective pitch commands instead.

And yes, it worked in a real emergency ! the heli didn’t have any rotors afterwards but we still had our heads.

So I guess you maybe saved my life ?

Thank you so very much. I used your guides heavily when working on my senior project doing linux networking at the system level many years ago!
beej! Wow! I read your guide in undergrad (many years ago, on the recommendation of a prof) and self-taught myself how networks work a year before my dedicated class on the subject. Your work truly inspired me then and I still fondly recall your work and advocate for others to read it.

That’s all. Thank you!

Thanks Beej! Like a lot of people here, your guide was extremely helpful in learning (and for reference!) network programming.
Your guide has helped me understand socket API when I was just starting with networking :)

Thank you for your existence.

Hey dude,

I just happened to reread the guide a couple of weeks ago. I hadn't done c socket programming in maybe a few years, and the guide refreshed my memory really well.

I've used it a few times over the years.

Thanks a lot!

Beej reminds me that the people who are the best at what they do, are having the most fun.

Thanks for your work Beej! I've referenced your pages many times over the years.

i dont know what was updated, but just want to say big thanks to beej

this guide was what got me into programming years ago

and i still refer to it from time to time

The last time I looked about 5 years ago it was formatted way different. Now has code boxes with highlights and easier to read.
Beej's Networking guide is probably the funniest software book I've ever read. There are parts in there that actually get me to laugh aloud. All while being a fantastic intro to sockets.

Couldn't recommend this more. Free online, plus you can purchase the paperback now.

Man, this is so cool -- an old-school programming resource that isn't encrusted with ads and trackers, just the information you want in HTML format. Kenton Lee's X programming guide was another such resource, but sadly that appears to have been eaten by the internet Langoliers.
When's the next update to cover io_uring?
Just wanted to say thanks. Your guides saved me during my OS/Networking class a few years back.