Windows Sockets in MFC
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Windows Sockets in MFC 

NoteNote

MFC supports Windows Sockets 1 but does not support Windows Sockets 2. Windows Sockets 2 first shipped with Windows 98 and is the version included with Windows 2000.

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.

For information on Windows Sockets, including an explanation of the socket concept, see 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. CAsyncSocket 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. For more information, see Windows Sockets: Socket Notifications.

  • CSocket

    This class, derived from CAsyncSocket, supplies a higher level abstraction for working with sockets through an MFC CArchive 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. CSocket 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 importantly, 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 Windows Sockets: Using Sockets with Archives and Windows Sockets: Using Class CAsyncSocket.

Windows Sockets DLLs

The Microsoft Windows operating systems supply the Windows Sockets dynamic-link libraries (DLL). Visual C++ supplies the appropriate header files and libraries and the Windows Sockets specification.

NoteNote

Under Windows NT and Windows 2000, 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.

For more information about Windows Sockets, see:

See Also

Show:
© 2016 Microsoft