Windows Sockets Network Programming Bob Quinn Dave Shute TT ADDISON-WESLEY PUBLISHING COMPANY Reading, Massachusetts Menlo Park, California New York Don Mills, Ontario Wokingham, England Amsterdam Bonn Sydney Singapore Tokyo Madrid San Juan Seoul Milan Mexico City Taipei
Contents Foreword xiii Preface xv Chapter 1 Introduction to Windows Sockets 1 What Is Windows Sockets? 2 What Is Its History? 4 What Are Its Benefits? 5 What Is Its Future? 9 Conclusion 10 Chapter 2 Windows Sockets Concepts 11 The OSI Network Model 12 The WinSock Network Model 14 OSI Layers in WinSock 16 Modular Boxes 24 Services and Protocols 26 Protocols and APIs 28 Chapter 3 TCP/IP Protocol Services 31 What Is TCP/IP? 32 What Is Its History? 34 Transport Services 35 Network Services 41 Support Protocols and Services 44 What Is Its Future? 46 Chapter 4 Network Application Mechanics 47 Client-Server Model 47 Network Program Sketch 49 vii
WINDOWS SOCKETS NETWORK PROGRAMMING Open a Socket 50 Name the Socket 53 Associate with Another Socket 58 Send and Receive between Sockets 64 Close the Socket 70 Client and Server Sketches 73 Chapter 5 Operation Modes 75 What Are Operation Modes? 76 Blocking Mode 78 Nonblocking Mode 92 Asynchronous Mode 99 Performance Differences 117 Platform Differences 117 Blocking Is Real 117 Chapter 6 Socket States 121 What Are the Socket States? 122 What Are the Methods of Detection? 125 WSAAsyncSelect( ) 129 select* ) 159 Peeking at Data 168 Out-of-Band Data 171 The Listening State 174 Chapter 7 Sample Application and Library 177 Sample FTP Client 178 Sample Library 234 WinSockx.h 251 Chapter 8 Host Names and Addresses 257 Hostname and Address Resolution 258 Host Table, DNS, and NIS 272
CONTENTS IX Local Host Information 275 Addresses and Formatting 277 Protocol and Address Families 282 Chapter 9 Socket Information and Control 285 Socket Control 285 Socket Options 290 Blocking Hooks 310 Socket Names 313 Chapter 10 Support Routines 319 Startup and Cleanup 319 Byte Ordering 329 Service Names and Ports 331 Protocol Names and Numbers 341 Error Reporting 349 Chapter 11 DLLs over WinSock 353 Creating a New API 354 DLL Issues 358 Sample DLL 363 Chapter 12 Porting from BSD Sockets 385 Differences to Consider 386 Sixteen-bit Windows Considerations 393 Incidentals 396 Functions List 398 Chapter 13 Debugging 405 Problem Types 406 What Failed, and How Did It Fail? 408 Problem Qualification 408
WINDOWS SOCKETS NETWORK PROGRAMMING Installation Debugging 409 Network Debugging 410 Application Debugging 413 Debugging Tools 414 Chapter 14 Dos and Don'ts 427 Characterizing Your Application 427 Application Data Flow 428 Stream Algorithms 433 Datagram Algorithms 438 Good-News Code 440 Common Traps and Pitfalls 444 Chapter 15 Platforms 459 The WOSA Network Model 460 Thirty-two-bit WinSock 461 Other P/atforms 469 Chapter 16 Optional Features 471 Optional Standard Is an Oxymoron 472 Should You Use Optional Features? 472 Raw Sockets 474 Multicast 481 Loopback 491 Sharing Sockets 491 Optional Options 492 Sockets as File Handles 492 Expect Any Error Anywhere 493 Other Optional Features 493 Chapter 17 WinSock 2 495 Do You Need WinSock 2? 496 Overview of Features 497
CONTENTS XI Multiple Protocol Support 499 Overlapped I/O 506 Scatter and Gather 509 Quality of Service 510 Socket Groups 511 Multipoint and Multicast 513 Conditional Acceptance 515 Connect and Disconnect Data 516 Socket Sharing 517 Protocol-specific Additions 518 Appendix A TCP/IP Protocol Headers 521 Layering within TCP/IP Packets 521 ARP Header 522 IP Header 523 ICMP Header 525 IGMP Header 526^ TCP Header 527 UDP Header 529 Sample Dialogs 529 Appendix B Quick Reference 539 Structures 539 Functions 545 Macros 556 Appendix C Error Reference 565 Where to Get Error Values 566 What Errors to Expect 567 User-fixable Errors 567 Detailed Error Descriptions 569 Errors in Numerical Order 595
XH WINDOWS SOCKETS NETWORK PROGRAMMING Appendix D What You Need 599 Essential Files 599 Compile and Link Mechanics 601 Using Different WinSocks 602 Using Different Languages 604 Appendix E Information Sources 605 Internet Sources 605 Bibliography 612 Index 621