Introduction
This book is for people who want to write programs that communicate with each other
using an application program interface (API) known as sockets. Some readers may be
very familiar with sockets already, as that model has become synonymous with network
programming. Others may need an introduction to sockets from the ground up. The
goal of this book is to offer guidance on network programming for beginners as well as
professionals, for those developing new network-aware applications as well as those
maintaining existing code, and for people who simply want to understand how the net-
working components of their system function.
All the examples in this text are actual, runnable code tested on Unix systems.
However, many non-Unix systems support the sockets API and the examples are
largely operating system-independent, as are the general concepts we present. Virtually
every operating system (OS) provides numerous network-aware applications such as
Web browsers, email clients, and file-sharing servers. We discuss the usual partitioning
of these applications into client and server and write our own small examples of these
many times throughout the text.
Presenting this material in a Unix-oriented fashion has the natural side effect of pro-
viding background on Unix itself, and on TCP/IP as well. Where more extensive back-
ground may be interesting, we refer the reader to other texts. Four texts are so com-
monly mentioned in this book that we've assigned them the following abbreviations:
. · APUE: Advanced Programming in the UNIX Environment [Stevens 1992]
· TCPv1: TCP//P Illustrated, Volume 1 [Stevens 1994]
· TCPv2: TCP//P Illustrated, Volume 2 [Wright and Stevens 1995]
· TCPv3: TCP//P Illustrated, Volume 3 [Stevens 1996]
TCPv2 contains a high level of detail very closely related to the material in this book, as
it describes and presents the actual 4.4BSD implementation of the network program-
ming functions for the sockets API (socket, bind, connect, and so on). If one under-
stands the implementation of a feature, the use of that feature in an application makes
more sense.
Changes from the Second Edition
Sockets have been around, more or less in their current form, since the 1980s, and it is a
tribute to their initial design that they have continued to be the network APl of choice.
Therefore, it may come as a surprise to learn that quite a bit has changed since the sec-
ond edition of this book was published in 1998. The changes we've made to the text are
summarized as follows:
· This new edition contains updated information on IPv6, which was only in draft
form at the time of publication of the second edition and has evolved somewhat.
· The descriptions of functions and the examples have all been updated to reflect
the most recent POSIX specification (POSIX 1003.1-2001), also known as the Single
Unix Specification Version 3.
· The coverage of the X/Open Transport Interface (XTI) has been dropped. That
API has fallen out of common use and even the most recent POSIX specification
does not bother to cover it.
· The coverage of TCP for transactions (T/TCP) has been dropped.
· Three chapters have been added to describe a relatively new transport protocol,
SCTE This reliable, message-oriented protocol provides multiple streams
between endpoints and transport-level support for multihoming. It was origi-
nally designed for transport of telephony signaling across the Intemet, but pro-
vides some features that many applications could take advantage of.
· A chapter has been added on key management sockets, which may be used with
Intemet Protocol Security (IPsec) and other network security services.
· The machines used, as well as the versions of their variants of Unix, have all
been updated, and the examples have been updated to reflect how these
machines behave. In many cases, examples were updated because OS vendors
fixed bugs or added features, but as one might expect, we've discovered the
occasional new bug here and there. The machines used for testing the examples
in this book were:
· Apple Power PC running MacOS/X 10.2.6
· HP PA-RISC running HP-UX lli
· IBM Power PC running AIX 5.1
· Intel x86 running FreeBSD 4.8
· Intel x86 running Linux 2.4.7
· Sun SPARC running FreeBSD 5.1
· Sun SPARC running Solaris 9
See Figure 1.16 for details on how these machines were used.
Volume 2 of this UNIX Network Programming series, subtitled Interprocess Communi-
cations, builds on the material presented here to cover message passing, synchroniza-
tion, shared memory, and remote procedure calls.
.Using This Book
This text can be used as either a tutorial on network programming or as a reference for
experienced programmers. When used as a tutorial or for an introductory class on net-
work programming, the emphasis should be on Part 2, "Elementary Sockets" (Chapters
3 through 11), followed by whatever additional topics are of interest. Part 2 covers the
basic socket functions for both TCP and UDP, along with SCTP, I/O multiplexing,
socket options, and basic name and address conversions. Chapter 1 should be read by
all readers, especially Section 1.4, which describes some wrapper functions used
throughout the text. Chapter 2 and perhaps Appendix A should be referred to as neces-
sary, depending on the reader's background. Most of the chapters in Part 3, "Advanced
Sockets," can be read independently of the others in that part of the book.
To aid in the use of this book as a reference, a thorough index is provided, along
with summaries on the end papers of where to find detailed descriptions of all the func-
tions and structures. To help those reading topics in a random order, numerous refer-
ences to related topics are provided throughout the text.
Source Code and Errata Availability
The source code for all the examples that appear in the book is available on the Web at
www. unpbook, com. The best way to learn network programming is to take these pro-
grams, modify them, and enhance them. Actually writing code of this form is the only
way to reinforce the concepts and techniques. Numerous exercises are also provided at
the end of each chapter, and most answers are provided in Appendix E.
A current errata for the book is also available from the same Web site.
Acknowledgments
The first and second editions of this book were written solely by W. Richard Stevens,
who passed away on September 1, 1999. His books have set a high standard and are
largely regarded as concise, laboriously detailed, and extremely readable works of art.
In providing this revision, the authors struggled to maintain the quality and thorough
coverage of Rich's earlier editions and any shortcomings in this area are entirely the
fault of the new authors.
The work of an author is only as good as the support from family members and
friends. Bill Fenner would like to thank his dear wife, Peggy (beach 1 4 mile champion),
and their housemate, Christopher Boyd for letting him off all his household chores
while working in the treehouse on this project. Thanks are also due to his friend, Jerry
Winner, whose prodding and encouragement were invaluable. Likewise, Andy Rudoff
wants to specifically thank his wife, Ellen, and girls, Jo and Katie, for their understand-
lng and encouragement throughout this project. We simply could not have done this
without all of you.
Randall Stewart with Cisco Systems, Inc. provided much of the SCTP material and
deserves a special acknowledgment for this much-valued contribution. The coverage of
this new and interesting topic simply would not exist without Randall's work.
The feedback from our reviewers was invaluable for catching errors, pointing out
areas that required more explanation, and suggesting improvements to our text and
code examples. The authors would like to thank: James Carlson, Wu-Chang Feng, Rick
Jones, Brian Kemighan, Sam Leffier, John McCann, Craig Metz, Ian Lance Taylor, David
Schwartz, and Gary Wright.
Numerous individuals and their organizations went beyond the normal call of duty
to provide either a loaner system, software, or access to a system, all of which were used
to test some of the examples in the text.
Jessie Haug of IBM Austin provided an AIX system and compilers.
Rick Jones and William Gilliam of Hewlett-Packard provided access to multiple
systems running HP-UX.
The staff at Addison Wesley has been a true pleasure to work with: Noreen Regina,
Kathleen Caren, Dan DePasquale, Anthony Gemellaro, and a very special thanks to our
editor, Mary Franz.
In a trend that Rich Stevens instituted (but contrary to popular fads), we produced
camera-ready copy of the book using the wonderful Groff package written by James
Clark, created the illustrations using the gpic program (using many of Gary Wright's
macros), produced the tables using the gtbl program, performed all the indexing, and
did the final page layout. Dave Hanson's loom program and some scripts by Gary
Wright were used to include the source code in the book. A set of awk scripts written
by Jon Bentley and Brian Kernighan helped in producing the final index.
The authors welcome electronic mail from any readers with comments, suggestions,
or bug fixes.
Bill Fenner Andrew M. Rudoff
Woodside, California Boulder, Colorado
October 2003
authors@unpbook, com
http: //www. unpbook, com