Unix Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI
Linux Journal, Issue 52, August 1998, 86-87.


Author:W. Richard Stevens
URL:http://www.kohala.com/~rstevens/
Publisher:Prentice Hall
URL:http://www.phptr.com/
Price:$59 US
ISBN:0-13-490012-X
Reviewer:David Bausum
Misc:Hardcover. Pages: xx, 1009. Copyright 1998.


In 1990 I chanced upon a review of a book which sounded interesting. I tucked the author's name and the book's title away and several months later, when I was in a large bookstore, asked if the volume was in stock. I was lead to the appropriate section, and there it was: Master and Commander by Patrick O'Brian. Within a dozen pages I was hooked. In the intervening years I've enjoyed the 6000+ pages in the 18-volume series dealing with the adventures of Maturin and Aubrey during the Napoleonic Wars. The point of this is that, when I find an author I like, I'll work my way through as many of his books as I can. Also, I've found the same approach works with technical books.

I've described in these pages (Issue 42) how I discovered W. Richard Stevens' Advanced Programming in the Unix® Environment. In conjunction with the project described in that review I needed to learn something about Unix network programming. By coincidence Stevens' first book was entitled Unix Network Programming. Without hesitation I ordered the book in late 1996 even though it was 6 years old and risked being dated. For reasons I don't quite understand (but put down to my own deficiencies) UNP and I never quite clicked in the way APUE and I did. Even so, when I learned in the Fall of 1997 that a second edition of UNP was about to be released, I ordered a copy as soon as it was available.

Generally, I am leery of new additions. All too often the book has a new preface, a new cover (with the words new or improved in big letters), a new copyright date, some cosmetic reshuffling of material, and little else. There are publishers and authors that specialize in that game. That is not what is going on here. The complete title gives a hint that the new edition is real (in fact three volumes are planned for the second edition). A cursory glance at the table of contents confirms that this is an entirely new book. Within several days of working with it something clicked, and the basics of Unix network programming fell into place for me.

I like working with Stevens' books because he makes beautiful books. The operable words here are ``makes beautiful.'' The second word is important because a beautiful book--one which is well organized and whose pages contain a balanced presentation of text (explaining the topic at hand), diagrams (showing relationships), tables (summarizing or detailing particular items), and code--makes the beginner's journey through new material much easier. The first word is important because a beautiful book does not just happen. It is made in the same way a Medieval work of art was made: by an individual who spent years mastering a field, and who then used that mastery in various projects. One of the tools Stevens uses is troff. If you have an interest in design and layout, you might profit from the part of Stevens' web site dealing with typesetting and troff resources. Lest the reader think I'm making too much of this, let me note that I'm currently working my way through several books in another area (best left unnamed). Unfortunately, none is as well organized (beautiful in the sense described above) as Stevens' books are. As a result I find each to be difficult to work with.

Before turning to the new book, here is a brief summary of the original one. UNP opens with a short introductory chapter and then spends about 160 pages on general (non-network) Unix programming. It continues with two chapters (80 pages) dealing with generalities about networking and descriptions of half-a-dozen network protocols. Finally, 260 pages into the book, the reader gets to an 80-page chapter dealing with Berkeley sockets (socket, bind, listen, ... ) and a 50-page chapter dealing with System V's transport layer interface. The book concludes with 300 pages (11 chapters) dealing with various network applications and totals 772 pages.

The new book is a complete rewrite of UNP's chapters on Berkeley sockets and System V's TLI. Although it is over 1000 pages long, it is not padded and it does not (unnecessarily) duplicate material from Stevens' earlier books. There is very little general (non-network) programming covered in the book. References are made as necessary to APUE. Similarly, Stevens refers to the TCP/IP series rather than duplicating material covered there. Also, he has managed to cut through the generalities about networking that occupied 10% of UNP. For example, on page 6 of the new book Stevens presents code for a working client program, and on page 13 he presents code for the complementary server. When a client connects, the server gets the time and sends it to the client who prints it and quits. The result is trivial (just as the classic `hello world' program is), but it provides a working client-server program less than a dozen pages into the book. By contrast I was always exhausted when I got to Berkeley sockets 250+ pages into UNP.

The new book mixes working, portable, real-world, annotated code with text, diagrams, and tables to document the sockets (and XTI) API. There are over 50 programs and over 100 functions discussed in the book.

Working code means you can obtain all the examples used in the book from Stevens' home page. For example, in less than an hour I downloaded the 226K tar.gz file, expanded it, ran Stevens' configuration routine, ran the makefile which builds the library used by all programs and subsequent makefiles, and compiled the programs for chapter 1. During the make of the library, I had a small problem because I don't have a threads library on my system. A quick change to the library makefile (removing references to threads) fixed things. I've not compiled all the programs supplied, but the ones I have compile and run the way the book says they should.

Portable means the code will compile not only on Linux systems (yes, Linux is one of the systems used as an example in the book), but on BSD, HP Unix, Digital Unix, Solaris (Sun/Sparc), and UnixWare systems. Some of these are running IPv4 protocol stacks, others are running IPv6. The code `doesn't care'; it is portable. One of the themes running through the book is how a programmer can write library routines which are protocol stack and system independent. Portable also means Posix compliance, particularly Draft 6.6 (March 1997) of P1003.1g.

Real-world means the code deals with errors. A programmer must constantly ask ``how can this code, this library call, this function fail? If it does, how can I either gracefully end the program or recover and keep going?'' For many new programmers this is a difficult lesson to learn. Stevens worries about this, and the beginner will benefit from his examples.

Annotated means that when code is discussed in the book, the printed listing includes the subdirectory and file name for the code, the lines in the listing are numbered, and the discussion in the text puts an appropriate line number range in the margin of the text. This makes it easy for you to move from text (discussing a range of lines) to the printed listing (in the book) to the actual file (on your hard disk).

The book contains much more than code. Stevens writes well. He has developed a style which allows him to interrupt his narrative discussion with historical and current observations. He does this by adjusting the margins and changing the font. It is easy to skip the `notes' on a first reading, and then to focus on them on subsequent passes through a section or chapter. One example of the timeliness of these notes is his explanation of denial of service attacks on page 99. When appropriate Stevens includes a diagram which provides an alternate explanation for a topic. Also, when appropriate he includes a table which summarizes similar pieces of a topic. The result is a work which is encyclopedic without being stifling.

I find I can approach a new chapter or section and first study the code, then examine the diagrams, then read the text, and then repeat the process possibly in a different order. As I understand more, the various approaches reinforce each other so that the whole ends up being greater than the sum of its parts. Finally, the various tables are natural places to refer back to once I understand a topic or when I have a specific question.

The book is organized into 4 parts. The first is 50 pages (2 chapters) of introductory material. This includes the working client-server program referred to above. The second is 200 pages (7 chapters). This discusses the basic networking functions utilized by Berkeley sockets (socket, bind, ... ). The part contains many programs and program fragments. The third is 500 pages (18 chapters) and deals with miscellaneous topics. The fourth is 120 pages (7 chapters) and covers the basic API for X/Open Transport Interface. The beginner will want to read the first two parts carefully. The more advanced reader should skim part 1 to get a sense of Stevens' style and then refer to part 2 as needed. To a large degree the chapters in part 3 are independent of each other. That means, once the basics from part 2 are clear, it is very easy to pick and choose among the topics discussed in part 3. If you are interested in threads, skip to chapter 23. If you are interested in IPv4 and IPv6, turn to chapters 10 and 24.

Rather than give more details here, I will invoke the spirit of Linux and refer you to Stevens' web site. If you look there, you will find information about each of Stevens' existing books, and you will find the table of contents, the preface, and a sample chapter of UNPv12e. In addition you will find the code for the book referred to above and a `readme' file describing installation of the software. Also, you will find the typesetting and troff references made above. Finally, you will find information about volume 2 (Interprocess Communication) and volume 3 (Applications). Both these volumes were still under construction at the time I wrote this review. Together the three volumes comprise the second edition of Unix Network Programming.

Author Summary

David Bausum received a Ph.D. in mathematics from Yale in 1974. Since the early 80's most of his energy has gone into software development and related activities. He coedited The Journal of Military History Cumulative Index: Vols 1-58, 1937-1994. He can be reached via e-mail at davidb@cfw.com.


Home

© Copyright 1998, David Bausum. All Rights Reserved.