WinSock Book

WinSock 2.0 is sold out. A second edition is scheduled to be released next year. will always have the latest information.


Available From
The publisher, IDG Books.
Barnes and Noble

Microsoft Windows clearly dominates the desktop PC market. The number of people connected to the global Internet continues to grow exponentially. The Windows Sockets (WinSock) API is the point where these two extraordinary technologies connect. That’s enough to make WinSock one of the most important programming interfaces developed this decade. But now, WinSock is more than that. With the introduction of version 2, and the additional capabilities it provides, WinSock promises to become the de facto standard for developing Windows network applications.

Developing Network Applications for Windows

Don't be misled by the book's title--it says "WinSock 2," , but since WinSock 2 encompasses all of the WinSock 1.1 API, you'll find what you need here to develop applications for either version. More importantly, the book shows you how to develop applications that work on both API sets, so that your code runs on as many WinSock implementations as possible.

Even though WinSock 2 is only available for Windows 95 and Windows NT 4, the WinSock 1.1 sample programs developed in this book will run on both16-bit and 32-bit Windows platforms. The WinSock 2 sample programs that are created in the last section of the book require Windows 95 or Windows NT 4.

This book is for anyone who wants to write any kind of network program for Windows. In the past, WinSock has been restricted to programming for TCP/IP networks. But this isn’t your father’s WinSock. WinSock 2 allows you to use the familiar socket interface to simultaneously access any number of installed transport protocols. So WinSock ain’t just for TCP/IP anymore. It’s well on its way to becoming the standard API for any type of network access from Windows.

I don’t assume that you have any prior knowledge of WinSock or network programming. Even if you’re not sure just exactly what WinSock is, you won't be overly challenged by the text and examples as long as you are familiar with C and Windows programming.

Even if you are a WinSock veteran, you’ll find useful information here--particularly in Part IV where the new functionality in WinSock 2 is discussed. I’ve also included tips and tricks that make WinSock programming easier, and documentation for some of the lesser-known, private interfaces.

WinSock Development Resources

The CD is packed with information related to WinSock programming. All of the latest WinSock specification documents are included, as well a complete set of RFC documents. That means RFC 1 (dated 4/7/1969) through RFC 2097 (dated 1/30/1997)

The sample programs include POP3 and SMTP clients, a general purpose TCP terminal, Ping and TraceRoute utilities (using both RAW sockets and Microsoft’s proprietary ICMP API), a fully-functioning Web server and protocol independent client and server applications.

The included SocketAdapter library makes it easy to discover — at runtime — what version of WinSock is installed and select the newest library. SocketAdapter applications can use the enhanced functionality available in WinSock 2 if it’s installed, yet still run using only WinSock 1.1 functions if it’s not. By using this library, a program can be not only protocol independent, but WinSock version independent as well.

Organization of This Book

The book is divided into four sections.

Part I - Introduction to Sockets

The first three chapters of the book efficiently summarize the necessary background information on networking in general and TCP/IP specifically. It also introduces the sockets programming model. Topics included are

  • The WinSock concept: What is it?
  • Fundamental concepts of networking
  • Protocol layers, specific Internet protocols, and the Internet standards process
  • The basics of programming with WinSock: the sockets paradigm, the roles of client and server applications, WinSock version negotiation, name resolution, and sending and receiving data with sockets.
  • Issues and challenges you'll encounter when using sockets with Windows. What is blocking and how do you avoid its negative effects?
  • Overview of specific functionality available in WinSock version 1.1 and in version 2.
  • Strategies that are available in each Windows platform.

Part II - The WinSock 1.1 API

Part II covers the portion of the API that is available in both versions 1.1 and 2 of WinSock. You'll find extensive coverage of the following topics:

  • The details of using sockets efficiently in applications that use the Windows graphical user interface. Asynchronous event notification is covered, as well as using the asynchronous database functions. You'll study a sample program that checks for the existence of waiting e-mail, using asynchronous mode and the asynchronous database functions.
  • An examination of "optional" functionality that is only available in some implementations of WinSock. You'll see development of a Ping utility, first using raw sockets and then using the proprietary Microsoft ICMP API. All of the socket options and ioctl commands available in WinSock 1.1 are included in this reference, whether they're "optional" or not.
  • A look at the functionality available in The CSocket and CAsyncSocket classes, comparing advantages and disadvantages of their use. A general-purpose TCP/IP terminal application is developed using the CAsyncSocket class; and a program that sends e-mail via SMTP is created using the CSocket class. The CSocket class is also extended to implement a time-out mechanism and allow the use of CString objects as parameters.

Part III - WinSock 2 API

The third section of the book is devoted to the functions, data structures, and strategies that are only available in version 2 of WinSock and therefore only available on Windows 95 and Windows NT 4. You'll explore the following:

  • The new functionality that is available in WinSock 2, including all of the new functions and options. We look at issues surrounding the porting of WinSock 1.1 applications to WinSock 2. There's discussion of new concepts introduced in WinSock 2: service providers, name space providers, and protocol types. Most prominent among WinSock 2’s new features is protocol independence, so we'll develop a protocol information utility that illustrates all of the information available from WinSock 2 about each installed transport protocol. A general-purpose function is also developed to ease selection of protocols at run time.
  • The new protocol-independent name registration and resolution functions that are used to advertise and locate services. A service query utility is developed that allows the user to find hosts and services by name using one of the many predefined GUIDs.
  • Advanced I/O topics such as overlapped I/O, scatter/gather, connect and disconnect data exchange, and quality of service. Several small programs are created that demonstrate the use of overlapped I/O with event objects and completion functions.

Part IV - Sample Applications

Part IV contains several complete, working example programs. It's here that you'll see demonstrated the key ideas developed in earlier sections of the book. I offer the following applications:

  • Asynchronous Server: a simple HTTP server application that can handle multiple simultaneous client connections by using asynchronous mode. The program uses only functionality that’s available in both version 1.1 and 2 of WinSock.
  • Multithreaded Server: the same HTTP server as Chapter 10, but this time using multiple threads. The program uses WinSock 2 specific functions and shows how blocking socket routines can be used in a multithreaded application without negatively affecting user feedback. The strategies used in this program can be applied to multithreaded client applications as well.
  • Protocol-Independent Client and Server: This demonstrates how multiple protocols can be used in WinSock 2 by creating an ECHO client and an ECHO server application that are both protocol-independent. The programs developed in this chapter explicitly link to WinSock. They can run on machines that only have the earlier WinSock 1.1 installed, yet they can still use the enhanced functionality of WinSock 2 if it is available.

Home | Example Source Code | WinSock Specifications | Development Files | Related Links