21-04-2012, 11:56 AM
Beej’s Guide to Network Programming Using Internet Sockets
beejnet.pdf (Size: 294.94 KB / Downloads: 54)
Intro
Hey! Socket programming got you down? Is this stuff just a little too difficult to figure out from the man pages? You
want to do cool Internet programming, but you don’t have time to wade through a gob of structs trying to figure
out if you have to call bind() before you connect(), etc., etc.
Well, guess what! I’ve already done this nasty business, and I’m dying to share the information with everyone!
You’ve come to the right place. This document should give the average competent C programmer the edge s/he needs
to get a grip on this networking noise.
Audience
This document has been written as a tutorial, not a reference. It is probably at its best when read by individuals who
are just starting out with socket programming and are looking for a foothold. It is certainly not the complete guide to
sockets programming, by any means.
Hopefully, though, it’ll be just enough for those man pages to start making sense... :-)
Platform and Compiler
The code contained within this document was compiled on a Linux PC using Gnu’s gcc compiler. It should, however,
build on just about any platform that uses gcc. Naturally, this doesn’t apply if you’re programming for Windows–see
the section on Windows programming, below.
. Official Homepage
This official location of this document is at California State University, Chico, at
Note for Solaris/SunOS Programmers
When compiling for Solaris or SunOS, you need to specify some extra command-line switches for linking in the
proper libraries. In order to do this, simply add "-lnsl -lsocket -lresolv" to the end of the compile command,
like so:
If you still get errors, you could try further adding a "-lxnet" to the end of that command line. I don’t know what
that does, exactly, but some people seem to need it.
Another place that you might find problems is in the call to setsockopt(). The prototype differs from that on my
Linux box, so instead of: