logo资料库

unix network programming(Volume 1 • Third Edition).pdf

第1页 / 共1021页
第2页 / 共1021页
第3页 / 共1021页
第4页 / 共1021页
第5页 / 共1021页
第6页 / 共1021页
第7页 / 共1021页
第8页 / 共1021页
资料共1021页,剩余部分请下载后查看
Copyright
Dedication
Addison-Wesley Professional Computing Series
Foreword
Preface
Introduction
Changes from the Second Edition
Using This Book
Source Code and Errata Availability
Acknowledgments
Introduction and TCP/IP
Introduction
Introduction
A Simple Daytime Client
Protocol Independence
Error Handling: Wrapper Functions
A Simple Daytime Server
Roadmap to Client/Server Examples in the Text
OSI Model
BSD Networking History
Test Networks and Hosts
Unix Standards
64-Bit Architectures
Summary
Exercises
The Transport Layer: TCP, UDP, and SCTP
Introduction
The Big Picture
User Datagram Protocol (UDP)
Transmission Control Protocol (TCP)
Stream Control Transmission Protocol (SCTP)
TCP Connection Establishment and Termination
TIME_WAIT State
SCTP Association Establishment and Termination
Port Numbers
TCP Port Numbers and Concurrent Servers
Buffer Sizes and Limitations
Standard Internet Services
Protocol Usage by Common Internet Applications
Summary
Exercises
Elementary Sockets
Sockets Introduction
Introduction
Socket Address Structures
Value-Result Arguments
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
Exercises
Elementary TCP Sockets
Introduction
socket Function
connect Function
bind Function
listen Function
accept Function
fork and exec Functions
Concurrent Servers
close Function
getsockname and getpeername Functions
Summary
Exercises
TCP Client/Server Example
Introduction
TCP Echo Server: main Function
TCP Echo Server: str_echo Function
TCP Echo Client: main Function
TCP Echo Client: str_cli Function
Normal Startup
Normal Termination
POSIX Signal Handling
Handling SIGCHLD Signals
wait and waitpid Functions
Connection Abort before accept Returns
Termination of Server Process
SIGPIPE Signal
Crashing of Server Host
Crashing and Rebooting of Server Host
Shutdown of Server Host
Summary of TCP Example
Data Format
Summary
Exercises
I/O Multiplexing: The select and poll Functions
Introduction
I/O Models
select Function
str_cli Function (Revisited)
Batch Input and Buffering
shutdown Function
str_cli Function (Revisited Again)
TCP Echo Server (Revisited)
pselect Function
poll Function
TCP Echo Server (Revisited Again)
Summary
Exercises
Socket Options
Introduction
getsockopt and setsockopt Functions
Checking if an Option Is Supported and Obtaining the Default
Socket States
Generic Socket Options
IPv4 Socket Options
ICMPv6 Socket Option
IPv6 Socket Options
TCP Socket Options
SCTP Socket Options
fcntl Function
Summary
Exercises
Elementary UDP Sockets
Introduction
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
Lost Datagrams
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
Exercises
Elementary SCTP Sockets
Introduction
Interface Models
sctp_bindx Function
sctp_connectx Function
sctp_getpaddrs Function
sctp_freepaddrs Function
sctp_getladdrs Function
sctp_freeladdrs Function
sctp_sendmsg Function
sctp_recvmsg Function
sctp_opt_info Function
sctp_peeloff Function
shutdown Function
Notifications
Summary
Exercises
SCTP Client/Server Example
Introduction
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
Controlling Termination
Summary
Exercises
Name and Address Conversions
Introduction
Domain Name System (DNS)
gethostbyname Function
gethostbyaddr Function
getservbyname and getservbyport Functions
getaddrinfo Function
gai_strerror Function
freeaddrinfo Function
getaddrinfo Function: IPv6
getaddrinfo Function: Examples
host_serv Function
tcp_connect Function
tcp_listen Function
udp_client Function
udp_connect Function
udp_server Function
getnameinfo Function
Re-entrant Functions
gethostbyname_r and gethostbyaddr_r Functions
Obsolete IPv6 Address Lookup Functions
Other Networking Information
Summary
Exercises
Advanced Sockets
IPv4 and IPv6 Interoperability
Introduction
IPv4 Client, IPv6 Server
IPv6 Client, IPv4 Server
IPv6 Address-Testing Macros
Source Code Portability
Summary
Exercises
Daemon Processes and the inetd Superserver
Introduction
syslogd Daemon
syslog Function
daemon_init Function
inetd Daemon
daemon_inetd Function
Summary
Exercises
Advanced I/O Functions
Introduction
Socket Timeouts
recv and send Functions
readv and writev Functions
recvmsg and sendmsg Functions
Ancillary Data
How Much Data Is Queued?
Sockets and Standard I/O
Advanced Polling
Summary
Exercises
Unix Domain Protocols
Introduction
Unix Domain Socket Address Structure
socketpair Function
Socket Functions
Unix Domain Stream Client/Server
Unix Domain Datagram Client/Server
Passing Descriptors
Receiving Sender Credentials
Summary
Exercises
Nonblocking I/O
Introduction
Nonblocking Reads and Writes: str_cli Function (Revisited)
Nonblocking connect
Nonblocking connect: Daytime Client
Nonblocking connect: Web Client
Nonblocking accept
Summary
Exercises
ioctl Operations
Introduction
ioctl Function
Socket Operations
File Operations
Interface Configuration
get_ifi_info Function
Interface Operations
ARP Cache Operations
Routing Table Operations
Summary
Exercises
Routing Sockets
Introduction
Datalink Socket Address Structure
Reading and Writing
sysctl Operations
get_ifi_info Function (Revisited)
Interface Name and Index Functions
Summary
Exercises
Key Management Sockets
Introduction
Reading and Writing
Dumping the Security Association Database (SADB)
Creating a Static Security Association (SA)
Dynamically Maintaining SAs
Summary
Exercises
Broadcasting
Introduction
Broadcast Addresses
Unicast versus Broadcast
dg_cli Function Using Broadcasting
Race Conditions
Summary
Exercises
Multicasting
Introduction
Multicast Addresses
Multicasting versus Broadcasting on a LAN
Multicasting on a WAN
Source-Specific Multicast
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
Exercises
Advanced UDP Sockets
Introduction
Receiving Flags, Destination IP Address, and Interface Index
Datagram Truncation
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
Exercises
Advanced SCTP Sockets
Introduction
An Autoclosing One-to-Many-Style Server
Partial Delivery
Notifications
Unordered Data
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
Controlling Timing
When to Use SCTP Instead of TCP
Summary
Exercises
Out-of-Band Data
Introduction
TCP Out-of-Band Data
sockatmark Function
TCP Out-of-Band Data Recap
Summary
Exercises
Signal-Driven I/O
Introduction
Signal-Driven I/O for Sockets
UDP Echo Server Using SIGIO
Summary
Exercises
Threads
Introduction
Basic Thread Functions: Creation and Termination
str_cli Function Using Threads
TCP Echo Server Using Threads
Thread-Specific Data
Web Client and Simultaneous Connections (Continued)
Mutexes: Mutual Exclusion
Condition Variables
Web Client and Simultaneous Connections (Continued)
Summary
Exercises
IP Options
Introduction
IPv4 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
Exercises
Raw Sockets
Introduction
Raw Socket Creation
Raw Socket Output
Raw Socket Input
ping Program
traceroute Program
An ICMP Message Daemon
Summary
Exercises
Datalink Access
Introduction
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
Exercises
Client/Server Design Alternatives
Introduction
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
Exercises
Streams
Introduction
Overview
getmsg and putmsg Functions
getpmsg and putpmsg Functions
ioctl Function
Transport Provider Interface (TPI)
Summary
Exercises
IPv4, IPv6, ICMPv4, and ICMPv6
Introduction
IPv4 Header
IPv6 Header
IPv4 Addresses
IPv6 Addresses
Internet Control Message Protocols (ICMPv4 and ICMPv6)
Virtual Networks
Introduction
The MBone
The 6bone
IPv6 Transition: 6to4
Debugging Techniques
System Call Tracing
Standard Internet Services
sock Program
Small Test Programs
tcpdump Program
netstat Program
lsof Program
Miscellaneous Source Code
unp.h Header
config.h Header
Standard Error Functions
Solutions to Selected Exercises
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 20
Chapter 21
Chapter 22
Chapter 24
Chapter 25
Chapter 26
Chapter 27
Chapter 28
Chapter 29
Chapter 30
Chapter 31
Bibliography
Stevens_title.fm Page i Tuesday, October 21, 2003 11:29 AM UNIX Network Programming The Sockets Networking API
Addison-Wesley Professional Computing Series Brian W. Kernighan and Craig Partridge, Consulting Editors Matthew H. Austern, Generic Programming and the STL: Using and Extending the C++ Standard Template Library David R. Butenhof, Programming with POSIX® Threads Brent Callaghan, NFS Illustrated Tom Cargill, C++ Programming Style William R. Cheswick/Steven M. Bellovin/Aviel D. Rubin, Firewalls and Internet Security, Second Edition: Repelling the Wily Hacker David A. Curry, UNIX® System Security: A Guide for Users and System Administrators Stephen C. Dewhurst, C++ Gotchas: Avoiding Common Problems in Coding and Design Erich Gamma/Richard Helm/Ralph Johnson/John Vlissides, Design Patterns: Elements of Reusable Object- Oriented Software Erich Gamma/Richard Helm/Ralph Johnson/John Vlissides, Design Patterns CD: Elements of Reusable Object- Oriented Software Peter Haggar, Practical Java™ Programming Language Guide David R. Hanson, C Interfaces and Implementations: Techniques for Creating Reusable Software Mark Harrison/Michael McLennan, Effective Tcl/Tk Programming: Writing Better Programs with Tcl and Tk Michi Henning/Steve Vinoski, Advanced CORBA® Programming with C++ Brian W. Kernighan/Rob Pike, The Practice of Programming S. Keshav, An Engineering Approach to Computer Networking: ATM Networks, the Internet, and the Telephone Network John Lakos, Large-Scale C++ Software Design Scott Meyers, Effective C++ CD: 85 Specific Ways to Improve Your Programs and Designs Scott Meyers, Effective C++, Second Edition: 50 Specific Ways to Improve Your Programs and Designs Scott Meyers, More Effective C++: 35 New Ways to Improve Your Programs and Designs Scott Meyers, Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library Robert B. Murray, C++ Strategies and Tactics David R. Musser/Gillmer J. Derge/Atul Saini, STL Tutorial and Reference Guide, Second Edition: C++ Programming with the Standard Template Library John K. Ousterhout, Tcl and the Tk Toolkit Craig Partridge, Gigabit Networking Radia Perlman, Interconnections, Second Edition: Bridges, Routers, Switches, and Internetworking Protocols Stephen A. Rago, UNIX® System V Network Programming Curt Schimmel, UNIX® Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers W. Richard Stevens/Bill Fenner/Andrew M. Rudoff, UNIX Network Programming Volume 1, Third Edition: The Sockets Networking API W. Richard Stevens, Advanced Programming in the UNIX® Environment W. Richard Stevens, TCP/IP Illustrated, Volume 1: The Protocols W. Richard Stevens, TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX® Domain Protocols W. Richard Stevens/Gary R. Wright, TCP/IP Illustrated Volumes 1-3 Boxed Set John Viega/Gary McGraw, Building Secure Software: How to Avoid Security Problems the Right Way Gary R. Wright/W. Richard Stevens, TCP/IP Illustrated, Volume 2: The Implementation Ruixi Yuan/ W. Timothy Strayer, Virtual Private Networks: Technologies and Solutions Visit www.awprofessional.com/series/professionalcomputing for more information about these titles.
Stevens_title.fm Page iii Tuesday, October 21, 2003 11:29 AM UNIX Network Programming The Sockets Networking API Volume 1 • Third Edition W. Richard Stevens Bill Fenner Andrew M. Rudoff Boston • San Francisco • New York • Toronto • Montreal London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City
Stevens_title.fm Page iv Tuesday, October 21, 2003 11:29 AM Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Addison- Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers discounts on this book when ordered in quantity for bulk purchases and special sales. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales (317) 581-3793 international@pearsontechgroup.com Visit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data A CIP catalog record for this book can be obtained from the Library of Congress. Copyright © 2004 by Pearson Education, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada. For information on obtaining permission for use of material from this work, please submit a written request to: Pearson Education, Inc. Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 ISBN: 0-13-141155-1 Text printed on recycled paper First printing
To Rich. Aloha nui loa.
Stevens_title.fm Page ii Tuesday, October 21, 2003 11:29 AM
Contents Foreword Preface Part 1. Introduction and TCP/IP Chapter 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 Chapter 2. 2.1 2.2 2.3 6 10 Independence Introduction Introduction A Simple Daytime Client Protocol Error Handling: Wrapper Functions A Simple Daytime Server Roadmap to Client/Server Examples in the Text OSI Model BSD Networ king Histor y Test Networ ks and Hosts Unix Standards 64-Bit Architectures Summary 20 22 11 25 28 13 3 18 29 The Transpor t Layer: TCP, UDP, and SCTP Introduction The Big Picture User Datagram Protocol (UDP) 32 34 31 16 xvii xix 1 3 31 vii
分享到:
收藏