BSD Sockets Programming From A Multi-Language Perspective,
Jones, M. Tim
A few weeks ago there was a
post on the OpenBSD Journal
about a new book on network programming. My interest was piqued as
I've done a fair amount of network programming in the past, but I had
a desire get back up to speed with it after a hiatus due to moving
and changing jobs. With that in mind I ordered BSD
Sockets Programming From A Multi-Language Perspective
(hereafter referred to as just BSP) so I
could refresh my skills.
The generally accepted Application Program Interface (API) for
network programming is the BSD Sockets API. This API is available in
most programming languages that offer networking and is very similar
between most implementations. There are programmers that can go a
whole career without ever writing one lick of networking code. This
is not a bad thing, but it reflects that while using networked
applications is commonplace, developing them isn't necessarily
something that every developer will do. However, if a programmer is
interested in developing networking code in one of several languages
then BSP is probably a good book to start with.
With every book that is trying to introduce a new user to concepts
it's best to have clear examples of the subject matter and
BSP is no slouch in that department. The book is
divided into three sections, an introduction to network programming,
sockets programming from a multi-language perspective, and software
patterns from a multi-language perspective, giving the user not only
a structured experience but also a good set of things they can turn
back to in the future when they are utilizing what they've learned.
In part one the networking model and all its layers are clearly
explained. Also discussed are Internet protocols, methods of
communication, and the model for socket programming. Jones then
proceeds to build up sockets piece by piece through code examples.
He starts simple, giving the reader all they need to build a network
application in perfectly sized chunks. By only giving the user the
fundamental parts of socket programming (socket(), bind(), listen(),
accept(), etc.) the user is made familiar with the concepts and
work flow of a client and a server communicating without getting
snowed over by too many details.
Later chapters in part one deal with more advanced topics of network
programming. More API calls (gethostbyname(), getsockopt(),
setsockopt(), etc.) are discussed that allow the user better control
and more choices when creating their applications. The discussion on
socket options was particularly clear and enlightening as I was
refreshing my knowledge. After making sure the reader has a chance
to be well grounded in the sockets API Jones then delves into much
more heady topics, but things programmers need to know. Jones
presents a lot of good advice on advanced topics, optimizations, and
pitfalls that I know I have used or been bitten by in the past.
Part two of BSP is all about actually using socket
programming APIs in a variety of languages. The book covers C, Java,
Perl, Python, TCL, and Ruby and with each one being it's own chapter
it does a good job of binding the socket API from part one to a
specific implementation. Each chapter also has a nifty section where
the origin and heritage of the language is given.
The third and final part of the book is all about software patterns.
Many common topics are discussed including Stream, Datagram,
Broadcast, and Multicast clients and servers; a simple web server;
and a simple mail server. The strongest point of this section is
that each language has it's own chapter and each software pattern is
discussed and code given, a definite plus for readers.
The Bottom Line
People that are interested in getting into network programming using
BSD sockets need a place to start. BSD Sockets
Programming is that place. This book could easily function as
a textbook, although it seemed to go very fast for me (however it's
been a long while since I've been in class and I already knew network
programming going in). Clear wording and not overly complex
explanations also help in conveying the subject matter. Many
sections of BSP can make for a good reference for the
average programmer and having it around so that you could hand
to someone that is looking for information is always a plus.
Jones provides excellent code samples (I only found one errata in my
read of the entire book, an assignment rather than a comparison in an
if statement and the author was very quick and gracious in his reply)
and there is an included CD-ROM that has tools and code on it to make
it easier to get started. One of the things that impressed me the
most was the awesome attention Jones placed on network byte order
versus host byte order. He clearly explains the situation and why
programmers should care, and then proceeds to hammer the need to make
sure data values are in network byte order in a lot of the examples.
The only area that could have been better covered is pitfalls with
thread programming. There is a brief mention of it at one point in
the book, but then there is no mention of either how to determine if
parts of the sockets API is thread safe or thread safe alternatives
for some of the calls (granted, thread programming is outside the
scoop of BSP but even having a reference to some source
of thread and network programming would have been cool).
One caveat should be stated here: BSP is for IPv4
programming only. Let me say that again, if you are interested in
IPv6 programming you won't find this book useful at all. Now, there
are two points to consider here. First, IPv4 is still the prominent
mode of networking on the Internet at large. IPv6 just hasn't taken
hold yet. Second is that IPv4 is much easier to learn on and there
is no reason to cloud a persons learning by throwing multiple IP
protocols at them. The W. Richard Stevens book Unix Network
Programming, Volume 1 (which Jones references heavily)
is a much more appropriate book for IPv6, but it is also a much
more heady and complex book for a beginner. For those just
starting out with little or no knowledge of network programming
BSP is a better choice (then you can step up to the
UNPvol1 when you are ready).
Last modified: Fri Nov 14 15:10:39 PST 2003