Redigera

Dela via


Windows Sockets in MFC

Note

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.

For more information about Windows Sockets, see:

See also

Windows Sockets