Share via


Windows Sockets in MFC

OverviewHow Do ISample

MFC supplies two models for writing network communications programs with Windows Sockets, embodied in two MFC classes. This article describes these models and further details MFC sockets support. A “socket” is an endpoint of communication — an object through which your application communicates with other Windows Sockets applications across a network.

This article describes:

  • Sockets programming models.

  • MFC socket samples and Windows Sockets DLLs.

  • Windows sockets articles.

For background information on Windows Sockets, including a detailed explanation of the socket concept, see the article Windows Sockets: Background.

Sockets Programming Models

The two MFC Windows Sockets programming models are supported by the following classes:

  • CAsyncSocket

    This class encapsulates the Windows Sockets API. is for programmers who know network programming and want the flexibility of programming directly to the sockets API but also want the convenience of callback functions for notification of network events. Other than packaging sockets in object-oriented form for use in C++, the only additional abstraction this class supplies is converting certain socket-related Windows messages into callbacks. (See the article Windows Sockets: Socket Notifications.)

  • CSocket

    This class, derived from CAsyncSocket, supplies a higher-level abstraction for working with sockets via an MFC object. Using a socket with an archive greatly resembles using MFC’s file serialization protocol. This makes it easier to use than the CAsyncSocket model. inherits many member functions from CAsyncSocket that encapsulate Windows Sockets APIs; you will have to use some of these functions and understand sockets programming generally. But CSocket manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket. Most important, CSocket provides blocking (with background processing of Windows messages), which is essential to the synchronous operation of CArchive.

Creating and using CSocket and CAsyncSocket objects is described in the articles Windows Sockets: Using Sockets with Archives and Windows Sockets: Using Class CAsyncSocket.

MFC Socket Samples and Windows Sockets DLLs

Visual C++ supplies the CHATTER and CHATSRVR sample applications to illustrate the client/server model, which is the most common model. CHATTER is the client; CHATSRVR is the server. These samples make good templates for writing your own clients and servers. You can access the source code for the MFC Advanced Concepts samples and .

The Microsoft Windows NT operating system supplies the Windows Sockets dynamic link libraries (DLLs). Visual C++ supplies the appropriate header files and libraries and the Windows Sockets specification. The specification is available in the Win32 SDK.

Note   Under Windows NT, Windows Sockets support for 16-bit applications is based on WINSOCK.DLL. For 32-bit applications, the support is in WSOCK32.DLL. The APIs provided are identical except that the 32-bit versions have parameters widened to 32 bits. Under Win32, thread safety is supplied.

ClassWizard does not support creating classes derived from the MFC Windows Sockets classes.

Windows Sockets Articles

For detailed information about Windows Sockets and their implementation in MFC, see the following articles: