Windows Sockets: Using Sockets with Archives

OverviewHow Do ISample

This article describes the CSocket programming model. Class supplies socket support at a higher level of abstraction than does class . CSocket uses a version of the MFC serialization protocol to pass data to and from a socket object via an MFC object. CSocket provides blocking (while managing background processing of Windows messages) and gives you access to CArchive, which manages many aspects of the communication that you would have to do yourself using either the raw API or class CAsyncSocket.

Tip   You can use class CSocket by itself, as a more convenient version of CAsyncSocket, but the simplest programming model is to use CSocket with a CArchive object.

For additional information about how the implementation of sockets with archives works, see the article Windows Sockets: How Sockets with Archives Work. For example code, see the articles Windows Sockets: Sequence of Operations and Windows Sockets: Example of Sockets Using Archives. For information about some of the functionality you can gain by deriving your own classes from the sockets classes, see the article Windows Sockets: Deriving from Socket Classes.

Caution   If you are writing an MFC client program to communicate with established (non-MFC) servers, don’t send C++ objects via the archive. Unless the server is an MFC application that understands the kinds of objects you want to send, it won’t be able to receive and deserialize your objects. For related material on the subject of communicating with non-MFC applications, also see the article Windows Sockets: Byte Ordering.

The CSocket Programming Model

Using a CSocket object involves creating and associating together several MFC class objects. In the general procedure below, each step is taken by both the server socket and the client socket, except for step 3, in which each socket type requires a different action.

Tip   At run time, the server application usually starts first in order to be ready and “listening” when the client application seeks a connection. If the server is not ready when the client tries to connect, you typically require the user application to try connecting again later.

To set up communication between a server socket and a client socket

  1. Construct a object.

  2. Use the object to create the underlying SOCKET handle.

    For a CSocket client object, you should normally use the default parameters to , unless you need a datagram socket. For a CSocket server object, you must specify a port in the Create call.

    Note   CArchive doesn’t work with datagram sockets. If you want to use CSocket for a datagram socket, you must use the class as you would CAsyncSocket — without an archive. Because datagrams are unreliable (not guaranteed to arrive and may be repeated or out of sequence), they aren’t compatible with serialization via an archive. You expect a serialization operation to complete reliably and in sequence. If you try to use CSocket with a CArchive object for a datagram, an MFC assertion fails.

  3. If the socket is a client, call to connect the socket object to a server socket.

    -or-

    If the socket is a server, call to begin listening for connect attempts from a client. Upon receiving a connection request, accept it by calling .

    Note   The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object.

  4. Create a object, associating the CSocket object with it.

  5. Create a object for either loading (receiving) or storing (sending) data. The archive is associated with the CSocketFile object.

    Keep in mind that CArchive doesn’t work with datagram sockets.

  6. Use the CArchive object to pass data between the client and server sockets.

    Keep in mind that a given CArchive object moves data in one direction only: either for loading (receiving) or storing (sending). In some cases, you’ll use two CArchive objects, one for sending data, the other for receiving acknowledgments.

    After accepting a connection and setting up the archive, you can perform such tasks as validating passwords.

  7. Destroy the archive, socket file, and socket objects.

Note   Class CArchive supplies the IsBufferEmpty member function specifically for use with class CSocket. If the buffer contains multiple data messages, for example, you need to loop until all of them are read and the buffer is cleared. Otherwise, your next notification that there is data to be received may be indefinitely delayed. Use IsBufferEmpty to assure that you retrieve all data. For examples of using IsBufferEmpty, see the sample application. For source code and information about MFC samples, see .

The article Windows Sockets: Sequence of Operations illustrates both sides of this process with example code.

What do you want to know more about?

See Also