News for hackers. Stuff that matters.
Re:Beej's Guide to Network Programming by tptacek on Wednesday April 29, 2009 @01:47AM (#584642)
Wow. I remember this thing. I'm pretty sure I was a senior in High School when it came out (1994?). Reading through it today is trippy.

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.

Re:Beej's Guide to Network Programming by beej on Wednesday April 29, 2009 @06:06PM (#585970)
Don't do "anything he says" because of the inclusion of the section on select()? Now, that's a little harsh, isn't it? After all, there's much more to the guide than that. I'll readily admit that select() is absolutely dated and inefficient, but it is also POSIX-compliant, and the guide is about (specifically) beginning network programming in (specifically) C, not event management or Python or tcl.

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.

Re:Beej's Guide to Network Programming by phugoid on Wednesday April 29, 2009 @11:05AM (#585149)
In all sincerity, I'd like to understand how and why you would use libevent instead of the approach outlined by Beej,

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".

Re:Beej's Guide to Network Programming by tptacek on Wednesday April 29, 2009 @01:36PM (#585477)
Beej's guide is really about writing synchronous network code. You connect a socket. Your program waits for the connect to complete. You write something to the socket. What, the write might only complete 10 of your 20 bytes? Wrap the write in a loop that keeps feeding more bytes to the system call! Ok, the request is completely sent? Great! Now write a loop that sits there waiting for all the data that we need to come back on the socket. Back and forth, back and forth, in lock-step.

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

Re:Beej's Guide to Network Programming by phugoid on Thursday April 30, 2009 @12:36AM (#586394)
Thanks for the crash course.
Re:Beej's Guide to Network Programming by davidw on Wednesday April 29, 2009 @06:19AM (#584828)
Or, if you're not attached to needing the speed that C provides, use something like Erlang, Tcl, or Python's twisted that has a nice event-based system built in.
Re:Beej's Guide to Network Programming by tptacek on Wednesday April 29, 2009 @01:39PM (#585490)
The irony is, if you use Twisted Python or EventMachine Ruby and pit it against Beej-style socket code, you're likely to wind up faster in Ruby or Python.