Table of Contents
Preface. 1. Introduction. A Few Conventions.
Road Map to the Rest of the Book.
Client-Server Architecture.
Basic Sockets API Review.
Summary.
2. Basics.
Tip 1: Understand the Difference between Connected and Connectionless Protocols.
Tip 2: Understand Subnets and CIDR.
Tip 3: Understand Private Addresses and NAT.
Tip 4: Develop and Use Application “Skeletons.”
Tip 5: Prefer the Sockets Interface to XTI/TLI.
Tip 6: Remember That TCP Is a Stream Protocol.
Tip 7: Don't Underestimate the Performance of TCP.
Tip 8: Avoid Reinventing TCP.
Tip 9: Realize That TCP Is a Reliable Protocol, Not an Infallible Protocol.
Tip 10: Remember That TCP/IP Is Not Polled.
Tip 11: Be Prepared for Rude Behavior from a Peer.
Tip 12: Don't Assume That a Successful LAN Strategy Will Scale to a WAN.
Tip 13: Learn How the Protocols Work.
Tip 14: Don't Take the OSI Seven-Layer Reference Model Too Seriously.
3. Building Effective and Robust Network Programs.
Tip 15: Understand the TCP Write Operation.
Tip 16: Understand the TCP Orderly Release Operation.
Tip 17: Consider Letting inetd Launch Your Application.
Tip 18: Consider Letting tcpmux “Assign” Your Server's Well-Known Port.
Tip 19: Consider Using Two TCP Connections.
Tip 20: Consider Making Your ApplicationsEvent Driven (1).
Tip 21: Consider Making Your Applications Event Driven (2).
Tip 22: Don't Use TIME-WAIT Assassination to Close a Connection.
Tip 23: Servers Should Set the SO_REUSEADDR Option.
Tip 24: When Possible, Use One Large Write Instead of Multiple Small Writes.
Tip 25: Understand How to Time Out a Connect Call.
Tip 26: Avoid Data Copying.
Tip 27: Zero the sockaddr_in Structure Before Use.
Tip 28: Don't Forget about Byte Sex.
Tip 29: Don't Hardcode IP Addresses or Port Numbers in Your Application.
Tip 30: Understand Connected UDP Sockets.
Tip 31: Remember That All the World's Not C.
Tip 32: Understand the Effects of Buffer Sizes.
4. Tools and Resources.
Tip 33: Become Familiar with the ping Utility.
Tip 34: Learn to Use tcpdump or a Similar Tool.
Tip 35: Learn to Use traceroute.
Tip 36: Learn to Use ttcp.
Tip 37: Learn to Use lsof.
Tip 38: Learn to Use netstat.
Tip 39: Learn to Use Your System's Call Trace Facility.
Tip 40: Build and Use a Tool to Capture ICMP Messages.
Tip 41: Read Stevens.
Tip 42: Read Code.
Tip 43: Visit the RFC Editor's Page.
Tip 44: Frequent the News Groups.
Appendix A: Miscellaneous UNIX Code.
etcp.h Header.
The daemon Function.
The signal Function.
Appendix B: Miscellaneous Windows Code.
The skel.h Header.
Windows Compatibility Routines.
Bibliography.
Index
Forewords & Introductions
The explosive growth of the Internet, wireless communications, and networking in general has led to a corresponding growth in the number of programmers and engineers writing networking applications. TCP/IP programming can seem seductively simple. The Application Programming Interface (API) is straightforward, and even the newest beginner can take a template for a client or server and flesh it out to a working application.
Often, however, after an initial surge of productivity, neophytes begin to bog down in details and find that their applications suffer from performance or robustness problems. Network programming is a field full of dark corners and often misunderstood details. This book sheds light into those corners and helps replace misunderstanding with an appreciation for the often subtle points of TCP/IP programming.
After finishing this book, you should have a thorough understanding of many of the trouble spots in network programming. In the text we examine many areas that seem only peripherally connected to the core knowledge that a working network programmer is expected to have. We will see, however, that by gaining an understanding of these minutiae, we also gain an appreciation for how the inner workings of the network protocols can interact with our applications. Armed with this insight, application behavior that previously seemed bewildering becomes understandable, and the solutions to problems become clear.
The organization of the text is a little unusual. We examine common problems one at a time in a series of tips. During the process of studying a particular trouble spot, we usually explore some aspect of TCP/IP programming in depth. When we finish, we willnot only have identified and dealt with common problems, we will also have a fairly comprehensive understanding of how the TCP/IP protocols work and interact with our applications.
The organization of the text into tips leads to a certain disjointedness. To help guide you, Chapter 1 contains a road map that explains the material covered in each chapter and how it all hangs together. The Contents, which lists each tip, will give you a sense of the text's organization. Because the title of each tip is in the form of an imperative, we can also think of the Contents as a list of network programming precepts.
On the other hand, this organization into tips makes the text more useful as a handbook. When we run into a problem in our day-to-day work, it is easy to revisit the appropriate tip to refresh our understanding of that particular problem area. You will find that many topics are visited in more than one tip, sometimes from a slightly different viewpoint. This repetition helps solidify the concepts and makes them seem more natural.
Readers
This text is written primarily for the advanced beginner or intermediate network programmer, but more experienced readers should find it useful as well. Although the reader is presumed to have a familiarity with networking and the basic sockets API, Chapter 1 contains a review of the elementary socket calls and uses them to build a primitive client and server. Tip 4 (Develop and Use Application Skeletons) revisits the various client and server models in more detail, so even a reader with minimal background should be able to understand and benefit from the text.
Almost all of the examples are written in the C language, so a reasonably good understanding of elementary C programming is necessary to get the full benefit from the programs in the text. In Tip 31 (Remember That All the World's Not C) we show some examples written in Perl, but no knowledge of Perl is assumed. Similarly, there are a few examples of small shell programs, but again, no previous experience with shell programming is necessary to understand them.
The examples and text attempt to be platform neutral. The examples are, with a few exceptions, intended to compile and run under any UNIX or Win32 system. Even those programmers not working on a UNIX or Windows system should have little trouble porting the examples to work on their platforms.