Beej's Guide to Network Programming
beej.us
beej.us
I had this document, the Trumpet Winsock manual, and a b-grade text on BSD sockets programming. I was foolish enough to have written socket code in assembly for a platform I have never programed for before, with an assembler that didn't support the opcodes I was typing in. This is all hard to imagine, FUCK, but I was 17, and I had a heart.
(considered a fool cuz I dropped outta high school, stereo type of another blackhat misunderstood, and it's still all good.)
Of course --- don't do anything he says anymore; all due respect (because I used to love this thing), but pick up libevent instead. Writing your own select() loop was malpractice in 1999.
Is there any link or other reference you could recommend for further study? The documentation on libevent's home page is big on "how", but not on "why".
Efficient network code just isn't written like this. It's scheduled, the same way processes are in the kernel. Even in threaded code, nothing sits there waiting on a socket.
Beej touches on this a little with his description of select(). But select isn't a utility function in socket programming. It's the kernel. And getting that loop completely right isn't trivial (timers alone can offer you 4-5 mainstream data structures to choose from). Moreover, select isn't the end-all kernel interface to scheduling I/O; if you're writing a server, it's probably much faster to use a more modern interface.
Libevent both hides the complexity of the event loop from you, and also abstracts it so you can drop in kqueue or epoll or whatever without being tied to a single old system call interface.
None of this is a knock against Beej's guide. I loved this thing, 15 years ago. =)
Plus, I just updated it to IPv6, man! Cut me some slack! :) I could take the select() thing out, but then I'd instantly get questions about it in the mail. But honestly, I'll take any serious feedback, including "give it up"--it is a time sink, and if it's not worth it, it's not worth it. Contact info's in the Guide.
All that being said, adding information about the inefficiencies of select() (and poll()) and a pointer to something better (e.g. libevent) would totally be a good addition. Additionally, a quick section about "why use C for network programming when every scripting language makes it easier" might be good (at least "because the rest of my project is written in C" comes to mind.) What do you think?
Are the other (preferably cross-platform) competitors to libevent that might be useful? I wouldn't talk about them in detail, because it's out of scope, but I definitely don't mind dropping pointers.
Now, you may think you're wising up to this. You might think, "What do I do if I have to change byte order on a char?" Then you might think, "Uh, never mind."
tpacek: writing networking code in C instead of Perl was malpractice in 1999, too. Except when it wasn't. You could make a reasonable argument that most things you would have done with sockets in 1994 should be done with a web server today...
You might like to follow up with this:
http://wangafu.net/~nickm/libevent-book/01_intro.html
Which is the introduction to this: