In this book, the authors offer unprecedented, start-to-finish guidance on making the most of sockets, the de facto standard for UNIX network programming. The authors begin by introducing virtually every basic capability of TCP and UDP sockets, including socket functions and options, I/O multiplexing, and name and address conversions. They present detailed coverage of the Posix.1g standard for sockets and the Posix threads. They also introduce advanced techniques for: establishing IPv4/IPv6 interoperability, implementing non-blocking I/O, routing sockets, broadcasting and multicasting, IP options, multithreading, advanced name and address conversions, UNIX domain protocols, and raw sockets.
Teaches students how to choose among today's leading client/server design approaches, including TCP iterative, concurrent, preforked and prethreaded servers.
The Internet/intranet revolution has dramatically increased the demand for students graduating with a sophisticated understanding of network programming APIs, especially sockets. This book helps students achieve that goal.
The classic guide to UNIX networking APIs - completely updated!
° Previous editions sold over 160,000 units! Second Edition (1998) sold over 53,000 in retail alone!
° Updates coverage of programming standards, debugging techniques, and covers Operating Systems including Red Hat 9, Solaris 9, HP-UX, Free BSD 4.8/5.0, AIX 5.x, and Mac OS X.
° Bill Fenner (AT/T Labs) and Andrew Rudoff (SUN) carry on the tradition of this great work.
New to this Edition
- Updates programming standards esp. POSIX (Single Unix Specification Version3), IPv6 APIs and IPSec APIs.
- Updates ways of debugging network programs.
- Covers Source Specific Multicast API-which many expect to lead way to wide deployment of IP multicast.
- OSes covered include: Free BSD 4.8/5.0,Linux Redhat 9.x, Solaris 9,AIX 5x HP-UX and Mac OS X 10
Table of Contents
I. INTRODUCTION AND TCP/IP. 1. Introduction.
A Simple Daytime Client.
Error Handling: Wrapper Functions.
A Simple Daytime Server.
Roadmap to Client/Server Examples in the Text.
BSD Networking History.
Test Networks and Hosts.
Summary. 2. The Transport Layer: TCP, UDP, and SCTP.
The Big Picture.
User Datagram Protocol (UDP).
Transmission Control Protocol (TCP).
Stream Control Transmission Protocol (SCTP).
TCP Connection Establishment and Termination.
SCTP Association Establishment and Termination.
TCP Port Numbers and Concurrent Servers.
Buffer Sizes and Limitations.
Standard Internet Services.
Protocol Usage by Common Internet Applications.
II. ELEMENTARY SOCKETS. 3. Sockets Introduction.
Socket Address Structures.
Byte Ordering Functions.
Byte Manipulation Functions.
inet_aton, inet_addr, and inet_ntoa Functions.
inet_pton and inet_ntop Functions.
sock_ntop and Related Functions.
readn, writen, and readline Functions.
Summary. 4. Elementary TCP Sockets.
fork and exec Functions.
getsockname and getpeername Functions.
Summary. 5. TCP Client/Server Example.
TCP Echo Server: main Function.
TCP Echo Server: str_echo Function.
TCP Echo Client: main Function.
TCP Echo Client: str_cli Function.
POSIX Signal Handling.
Handling SIGCHLD Signals.
wait and waitpid Functions.
Connection Abort before accept Returns.
Termination of Server Process.
Crashing of Server Host.
Crashing and Rebooting of Server Host.
Shutdown of Server Host.
Summary of TCP Example.
Summary. 6. I/O Multiplexing: The select and poll Functions.
str_cli Function (Revisited).
Batch Input and Buffering.
str_cli Function (Revisited Again).
TCP Echo Server (Revisited).
TCP Echo Server (Revisited Again).
Summary. 7. Socket Options.
getsockopt and setsockopt Functions.
Checking if an Option Is Supported and Obtaining the Default.
Generic Socket Options.
IPv4 Socket Options.
ICMPv6 Socket Option.
IPv6 Socket Options.
TCP Socket Options.
SCTP Socket Options.
Summary. 8. Elementary UDP Sockets.
recvfrom and sendto Functions.
UDP Echo Server: main Function.
UDP Echo Server: dg_echo Function.
UDP Echo Client: main Function.
UDP Echo Client: dg_cli Function.
Verifying Received Response.
Server Not Running.
Summary of UDP Example.
connect Function with UDP.
dg_cli Function (Revisited).
Lack of Flow Control with UDP.
Determining Outgoing Interface with UDP.
TCP and UDP Echo Server Using select.
Summary. 9. Elementary SCTP Sockets.
Summary. 10. SCTP Client/Server Example.
SCTP One-to-Many-Style Streaming Echo Server: main Function.
SCTP One-to-Many-Style Streaming Echo Client: main Function.
SCTP Streaming Echo Client: str_cli Function.
Exploring Head-of-Line Blocking.
Controlling the Number of Streams.
Summary. 11. Name and Address Conversions.
Domain Name System (DNS).
getservbyname and getservbyport Functions.
getaddrinfo Function: IPv6.
getaddrinfo Function: Examples.
gethostbyname_r and gethostbyaddr_r Functions.
Obsolete IPv6 Address Lookup Functions.
Other Networking Information.
III. ADVANCED SOCKETS. 12. IPv4 and IPv6 Interoperability.
IPv4 Client, IPv6 Server.
IPv6 Client, IPv4 Server.
IPv6 Address-Testing Macros.
Source Code Portability.
Summary. 13. Daemon Processes and the inetd Superserver.
Summary. 14. Advanced I/O Functions.
recv and send Functions.
readv and writev Functions.
recvmsg and sendmsg Functions.
How Much Data Is Queued?
Sockets and Standard I/O.
Summary. 15. Unix Domain Protocols.
Unix Domain Socket Address Structure.
Unix Domain Stream Client/Server.
Unix Domain Datagram Client/Server.
Receiving Sender Credentials.
Summary. 16. Nonblocking I/O.
Nonblocking Reads and Writes: str_cli Function (Revisited).
Nonblocking connect: Daytime Client.
Nonblocking connect: Web Client.
Summary. 17. ioctl Operations.
ARP Cache Operations.
Routing Table Operations.
Summary. 18. Routing Sockets.
Datalink Socket Address Structure.
Reading and Writing.
get_ifi_info Function (Revisited).
Interface Name and Index Functions.
Summary. 19. Key Management Sockets.
Reading and Writing.
Dumping the Security Association Database (SADB).
Creating a Static Security Association (SA).
Dynamically Maintaining SAs.
Summary. 20. Broadcasting.
Unicast versus Broadcast.
dg_cli Function Using Broadcasting.
Summary. 21. Multicasting.
Multicasting versus Broadcasting on a LAN.
Multicasting on a WAN.
Multicast Socket Options.
mcast_join and Related Functions.
dg_cli Function Using Multicasting.
Receiving IP Multicast Infrastructure Session Announcements.
Sending and Receiving.
Simple Network Time Protocol (SNTP).
Summary. 22. Advanced UDP Sockets.
Receiving Flags, Destination IP Address, and Interface Index.
When to Use UDP Instead of TCP.
Adding Reliability to a UDP Application.
Binding Interface Addresses.
Concurrent UDP Servers.
IPv6 Packet Information.
IPv6 Path MTU Control.
Summary. 23. Advanced SCTP Sockets.
An Autoclosing One-to-Many-Style Server.
Binding a Subset of Addresses.
Determining Peer and Local Address Information.
Finding an Association ID Given an IP Address.
Heartbeating and Address Failure.
Peeling Off an Association.
When to Use SCTP Instead of TCP.
Summary. 24. Out-of-Band Data.
TCP Out-of-Band Data.
TCP Out-of-Band Data Recap.
Summary. 25. Signal-Driven I/O.
Signal-Driven I/O for Sockets.
UDP Echo Server Using SIGIO.
Summary. 26. Threads.
Basic Thread Functions: Creation and Termination.
str_cli Function Using Threads.
TCP Echo Server Using Threads.
Web Client and Simultaneous Connections (Continued).
Mutexes: Mutual Exclusion.
Web Client and Simultaneous Connections (Continued).
Summary. 27. IP Options.
IPv4 Source Route Options.
IPv6 Extension Headers.
IPv6 Hop-by-Hop Options and Destination Options.
IPv6 Routing Header.
IPv6 Sticky Options.
Historical IPv6 Advanced API.
Summary. 28. Raw Sockets.
Raw Socket Creation.
Raw Socket Output.
Raw Socket Input.
An ICMP Message Daemon.
Summary. 29. Datalink Access.
BSD Packet Filter (BPF).
Datalink Provider Interface (DLPI).
Linux: SOCK_PACKET and PF_PACKET.
libpcap: Packet Capture Library.
libnet: Packet Creation and Injection Library.
Examining the UDP Checksum Field.
Summary. 30. Client/Server Design Alternatives.
TCP Client Alternatives.
TCP Test Client.
TCP Iterative Server.
TCP Concurrent Server, One Child per Client.
TCP Preforked Server, No Locking Around accept.
TCP Preforked Server, File Locking Around accept.
TCP Preforked Server, Thread Locking Around accept.
TCP Preforked Server, Descriptor Passing.
TCP Concurrent Server, One Thread per Client.
TCP Prethreaded Server, per-Thread accept.
TCP Prethreaded Server, Main Thread accept.
Summary. 31. STREAMS.
getmsg and putmsg Functions.
getpmsg and putpmsg Functions.
Transport Provider Interface (TPI).
Summary. Appendix A. IPv4, IPv6, ICMPv4, and ICMPv6.
Internet Control Message Protocols (ICMPv4 and ICMPv6). Appendix B. Virtual Networks.
IPv6 Transition: 6to4. Appendix C. Debugging Techniques.
System Call Tracing.
Standard Internet Services.
Small Test Programs.
lsof Program. Appendix D. Miscellaneous Source Code.
Standard Error Functions. Appendix E. Solutions to Selected Exercises. Bibliography. Index.
UNIX Network Programming, Volume 1: The Sockets Networking API, Third Edition
"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."
--Sam LefflerThe classic guide to UNIX networking APIs... now completely updated!
To build today's highly distributed, networked applications and services, you need deep mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment: UNIX Network Programming, Volume 1, Third Edition.
Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:
- POSIX Single UNIX Specification Version 3
- IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability)
- The new SCTP transport protocol
- IPsec-based Key Management Sockets
- FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations
- New network program debugging techniques
- Source Specific Multicast API, the key enabler for widespread IP multicast deployment
The authors also update and extend Stevens' definitive coverage of these crucial UNIX networking standards and techniques:
- Sockets: elementary, advanced, routed, and raw
- I/O: multiplexing, advanced functions, nonblocking, and signal-driven
- Broadcasting and multicasting
- Design: TCP iterative, concurrent, preforked, and prethreaded servers
Since 1990, network programmers have turned to one source for the insights and techniques they need: W. Richard Stevens' UNIX Network Programming. Now, there's an edition specifically designed for today's challenges--and tomorrow's.
The late W. RICHARD STEVENS was the original author of UNIX Network Programming, First and Second Editions, widely recognized as the classic texts in UNIX networking. BILL FENNER is Principal Technical Staff Member at AT&T Labs in Menlo Park, CA, specializing in IP multicasting, network management, and measurement. He is one of the IETF's Routing Area Directors, responsible for approving all routing-related documents that get published as RFCs. ANDREW M. RUDOFF, Senior Software Engineer at Sun Microsystems, specializes in networking, operating systems internals, file systems, and high availability software architecture.