• Aucun résultat trouvé

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 programs, 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.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"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 Leffler

The 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 [ Team LiB ]

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 ¼ 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 understanding 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 Kernighan, Sam Leffler, 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.

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"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 Leffler

The 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 authors@unpbook.com

http://www.unpbook.com

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"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 Leffler

The 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 [ Team LiB ]

Part 1: Introduction and TCP/IP

Chapter 1. Introduction

Chapter 2. The Transport Layer: TCP, UDP, and SCTP [ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"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 Leffler

The 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 [ Team LiB ]

Chapter 1. Introduction

Section 1.1. Introduction

Section 1.2. A Simple Daytime Client Section 1.3. Protocol Independence

Section 1.4. Error Handling: Wrapper Functions Section 1.5. A Simple Daytime Server

Section 1.6. Roadmap to Client/Server Examples in the Text Section 1.7. OSI Model

Section 1.8. BSD Networking History Section 1.9. Test Networks and Hosts Section 1.10. Unix Standards

Section 1.11. 64-Bit Architectures Section 1.12. Summary

Exercises [ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"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 Leffler

The 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 [ Team LiB ]

1.1 Introduction

When writing programs that communicate across a computer network, one must first invent a protocol, an agreement on how those programs will communicate. Before delving into the design details of a protocol, high-level decisions must be made about which program is expected to initiate communication and when responses are expected. For example, a Web server is typically thought of as a long-running program (or daemon) that sends network messages only in response to requests coming in from the network. The other side of the protocol is a Web client, such as a browser, which always initiates communication with the server. This organization into client and server is used by most network-aware applications.

Deciding that the client always initiates requests tends to simplify the protocol as well as the programs themselves. Of course, some of the more complex network applications also require asynchronous callback communication, where the server initiates a message to the client. But it is far more common for applications to stick to the basic client/server model shown in Figure 1.1.

Figure 1.1. Network application: client and server.

Clients normally communicate with one server at a time, although using a Web browser as an example, we might communicate with many different Web servers over, say, a 10-minute time period. But from the server's perspective, at any given point in time, it is not unusual for a server to be communicating with multiple clients. We show this in Figure 1.2. Later in this text, we will cover several different ways for a server to handle multiple clients at the same time.