C++ Network Programming Volume 1 The C++ In-Depth Series Bjarne Stroustrup, Editor “I have made this letter longer than usual, because I lack the time to make it short.” —BLAISEPASCAL The advent of the ISO/ANSI C++ standard marked the beginning of a new era for C++ programmers. The standard offers many new facilities and opportunities, but how can a real-world programmer find the time to discover the key nuggets of wisdom within this mass of information? The C++ In-Depth Seriesminimizes learning time and confusion by giving programmers concise, focused guides to specific topics. Each book in this series presents a single topic, at a technical level appropriate to that topic. The Series’ practical approach is designed to lift professionals to their next level of programming skills. Written by experts in the field, these short, in-depth monographs can be read and referenced without the distraction of unrelated material. The books are cross-referenced within the Series, and also reference The C++ Programming Languageby Bjarne Stroustrup. As you develop your skills in C++, it becomes increasingly important to separate essential information from hype and glitz, and to find the in-depth content you need in order to grow. The C++ In-Depth Series provides the tools, concepts, techniques, and new approaches to C++ that will give you a critical edge. Titles in the Series Accelerated C++:Practical Programming by Example, Andrew Koenig and Barbara E. Moo Applied C++: Practical Techniques for Building Better Software, Philip Romanik and Amy Muntz The Boost Graph Library: User Guide and Reference Manual, Jeremy G. Siek, Lie-Quan Lee, and Andrew Lumsdaine C++ In-Depth Box Set, Bjarne Stroustrup, Andrei Alexandrescu, Andrew Koenig, Barbara E. Moo, Stanley B. Lippman, and Herb Sutter C++ Network Programming, Volume 1: Mastering Complexity Using ACE and Patterns, Douglas C. Schmidt and Stephen D. Huston C++ Network Programming, Volume 2: Systematic Reuse with ACE and Frameworks, Douglas C. Schmidt and Stephen D. Huston Essential C++, Stanley B. Lippman Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions, Herb Sutter Modern C++ Design: Generic Programming and Design Patterns Applied, Andrei Alexandrescu More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions, Herb Sutter For more information, check out the series Web site at http://www.awprofessional.com/series/indepth/ C++ Network Programming Volume 1 Mastering Complexity with ACE and Patterns Douglas C. Schmidt Stephen D. Huston Boston San Francisco New York Toronto Montreal (cid:0) (cid:0) (cid:0) (cid:0) London Munich Paris Madrid (cid:0) (cid:0) (cid:0) Capetown Sydney Tokyo Singapore Mexico City (cid:0) (cid:0) (cid:0) (cid:0) Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedastrademarks.Wherethose designationsappearinthisbook,andAddison-Wesleywasawareofatrademarkclaim,thedesignationshavebeenprintedwith initialcapitallettersorinallcapitals. Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedorimpliedwarrantyofanykind andassumenoresponsibilityforerrorsoromissions.Noliabilityisassumedforincidentalorconsequentialdamagesin connectionwithorarisingoutoftheuseoftheinformationorprogramscontainedherein. Thepublisheroffersdiscountsonthisbookwhenorderedinquantityforbulkpurchasesandspecialsales.Formoreinformation, pleasecontact: U.S.CorporateandGovernmentSales (800) 382-3419 [email protected] ForsalesoutsideoftheU.S.,pleasecontact: InternationalSales [email protected] VisitAddison-WesleyontheWebatwww.awprofessional.com LibraryofCongressCataloging-in-PublicationData Schmidt,DouglasC. C++networkprogramming/DouglasC.Schmidt,StephenD.Huston. p.cm. Includesbibliographicalreferencesandindex. Contents:Vol.1.MasteringcomplexitywithACEandpatterns. ISBN0-201-60464-7(v.1:pbk.) 1.C++(Computerprogramlanguage)2.Object-orientedprogramming(Computer science)3.Computernetworks.I.Huston,StephenD.II.Title. QA76.73.C153S3682002 005.2'762-dc21 2001053345 Copyright©2002byAddison-Wesley Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,inanyform,orby anymeans,electronic,mechanical,photocopying,recording,orotherwise,withoutthepriorconsentofthepublisher.Printedin theUnitedStatesofAmerica.PublishedsimultaneouslyinCanada. Forinformationonobtainingpermissionforuseofmaterialfromthiswork,pleasesubmitawrittenrequestto: PearsonEducation,Inc. RightsandContractsDepartment 75ArlingtonStreet,Suite300 Boston,MA 02116 Fax:(617)848-7047 ISBN 0201604647 Text printed on recycled paper 456789—OPM—0706050403 4thPrinting,November2004 Contents List of Figures ix Foreword xi About This Book xv Chapter 0 Design Challenges, Middleware Solutions, and ACE 1 0.1 Challenges of Networked Applications 1 0.2 Networked Application Design Dimensions 5 0.3 Object-Oriented Middleware Solutions 7 0.4 An Overview of the ACE Toolkit 12 0.5 Example: A Networked Logging Service 17 0.6 Summary 19 Part I Object-Oriented Network Programming 21 Chapter 1 Communication Design Dimensions 23 1.1 Connectionless versus Connection-Oriented Protocols 23 1.2 Synchronous versus Asynchronous Message Exchange 26 1.3 Message Passing versus Shared Memory 28 1.4 Summary 31 v vi CONTENTS Chapter 2 An Overview of the Socket API 33 2.1 An Overview of Operating System IPC Mechanisms 33 2.2 The Socket API 34 2.3 Limitations of the Socket API 37 2.4 Summary 43 Chapter 3 The ACE Socket Wrapper Facades 45 3.1 Overview 45 3.2 The ACE Addr and ACE INET Addr Classes 49 3.3 The ACE IPC SAP Class 52 3.4 The ACE SOCK Class 54 3.5 The ACE SOCK Connector Class 56 3.6 The ACE SOCK IO and ACE SOCK Stream Classes 60 3.7 The ACE SOCK Acceptor Class 64 3.8 Summary 68 Chapter 4 Implementing the Networked Logging Service 71 4.1 Overview 71 4.2 The ACE Message Block Class 72 4.3 The ACE InputCDR and ACE OutputCDR Classes 76 4.4 The Initial Logging Server 80 4.5 The Client Application 95 4.6 Summary 99 Part II Concurrent Object-Oriented Network Programming 101 Chapter 5 Concurrency Design Dimensions 103 5.1 Iterative, Concurrent, and Reactive Servers 103 5.2 Processes versus Threads 109 5.3 Process/Thread Spawning Strategies 112 5.4 User, Kernel, and Hybrid Threading Models 114 5.5 Time-Shared and Real-Time Scheduling Classes 119 5.6 Task- versus Message-Based Architectures 121 5.7 Summary 123 CONTENTS vii Chapter 6 An Overview of Operating System Concurrency Mechanisms 125 6.1 Synchronous Event Demultiplexing 125 6.2 Multiprocessing Mechanisms 127 6.3 Multithreading Mechanisms 129 6.4 Synchronization Mechanisms 130 6.5 Limitations with OS Concurrency Mechanisms 135 6.6 Summary 137 Chapter 7 The ACE Synchronous Event Demultiplexing Wrapper Facades 139 7.1 Overview 139 7.2 The ACE Handle Set Class 141 7.3 The ACE Handle Set Iterator Class 147 7.4 The ACE::select() Methods 151 7.5 Summary 157 Chapter 8 The ACE Process Wrapper Facades 159 8.1 Overview 159 8.2 The ACE Process Class 161 8.3 The ACE Process Options Class 165 8.4 The ACE Process Manager Class 169 8.5 Summary 182 Chapter 9 The ACE Threading Wrapper Facades 185 9.1 Overview 185 9.2 The ACE Thread Manager Class 187 9.3 The ACE Sched Params Class 198 9.4 The ACE TSS Class 201 9.5 Summary 205 Chapter 10 The ACE Synchronization Wrapper Facades 207 10.1 Overview 207 10.2 The ACE Guard Classes 209 10.3 The ACE Mutex Classes 212 10.4 The ACE Readers/Writer Lock Classes 218 10.5 The ACE Semaphore Classes 222 10.6 The ACE Condition Variable Classes 229 10.7 Summary 233 viii CONTENTS Appendix A Design Principles for ACE C++ Wrapper Facades 235 A.1 Overview 235 A.2 Use Wrapper Facades to Enhance Type Safety 236 A.3 Simplify for the Common Case 238 A.4 Use Hierarchies to Enhance Design Clarity and Extensibility 246 A.5 Hide Platform Differences Whenever Possible 248 A.6 Optimize for Efficiency 255 A.7 Summary 257 Appendix B The Past, Present, and Future of ACE 259 B.1 The Evolution of ACE 259 B.2 The Road Ahead 267 B.3 Concluding Remarks 268 Glossary 269 Bibliography 285 Index 295 List of Figures 0.1 A Stand-alone Application Architecture. . . . . . . . . . . . . 2 0.2 A Common Networked Application Environment . . . . . . . 3 0.3 Networked Application Design Dimensions . . . . . . . . . . . 6 0.4 Object-Oriented Middleware Layers in Context . . . . . . . . 8 0.5 The Layered Architecture of ACE . . . . . . . . . . . . . . . . 13 0.6 Participants in the Networked Logging Service . . . . . . . . . 18 1.1 Alternative Connection Multiplexing Strategies . . . . . . . . 25 1.2 Synchronous versus Asynchronous Messaging Strategies . . 26 1.3 Message Passing versus Shared Memory . . . . . . . . . . . . 29 2.1 Taxonomy of Socket Dimensions . . . . . . . . . . . . . . . . 42 3.1 The ACE Connection-Oriented Socket Class Relationships . 47 3.2 Taxonomy of the ACE Socket Wrapper Facades . . . . . . . . 48 3.3 Roles in the ACE Socket Wrapper Facade . . . . . . . . . . . 49 3.4 The ACE Addr and ACE INET Addr Class Diagrams . . . . . . 51 3.5 The ACE IPC SAP and ACE SOCK Class Diagrams . . . . . . . 54 3.6 The ACE SOCK Connector Class Diagram . . . . . . . . . . . . 57 3.7 The ACE SOCK Stream and ACE SOCK IO Class Diagrams . . . 61 3.8 The ACE SOCK Acceptor Class Diagram . . . . . . . . . . . . 65 4.1 The ACE Message Block Class Diagram . . . . . . . . . . . . 73 4.2 Two Kinds of ACE Message Block . . . . . . . . . . . . . . . . 75 ix