This article explains how to combine CSocket objects, CSocketFile objects, and CArchive objects to simplify data sending and receiving through Windows Sockets.
Windows Sockets: An example of a socket with an archive filePacketSerializeFunction.PacketSerializeThe working mechanism of the archive object in the example is very similar to that of the archive object passed to the MFC Serialize function. The basic difference between them is that for sockets, archives are not attached to standard CFile objects (usually associated with disk files), butCSocketFileObject.CSocketFileThe object is not connected to a disk file, butCSocketObject.
OneCArchiveObject To manage a buffer. When the buffer for storing (sending) archives is full, the associatedCFileThe buffer content written by the object. Refreshing the Archive Buffer attached to the socket is equivalent to sending messages. When the buffer for loading (receiving) archives is full,CFileStop reading the object until the buffer is available again.
CSocketFileClass slaveCFileBut it does not support CFile member functions, such as locating functions.Seek,GetLength,SetLengthWait, lock the FunctionLockRangeAndUnlockRange, OrGetPositionFunction. Each CSocketFile object must write or read the byte sequence into the associatedCSocketObject, or write or read the byte sequence from this object. Because files are not involved,SeekAndGetPositionAnd so on.CSocketFileSlaveCFileTherefore, it generally inherits all these member functions. To prevent this situationCSocketFileUnsupported RewritingCFileThe member function causes CNotSupportedException.
CSocketFileThe object calls itsCSocketTo send or receive data.
Shows the relationship between these objects at both ends of the communication.
CArchive, CSocketFile, and CSocket
This looks complicated, so that you do not have to manage socket details in person. You create sockets, files, and archives, and then start sending or receiving data by inserting data into the archive or extracting data from the archive. Details of the CArchive, CSocketFile, and CSocket management backend.
CSocketAn object is actually a two-state object: Sometimes asynchronous (usually state) and sometimes synchronous. When asynchronous, the socket can receive asynchronous notifications from the framework. However, during an operation (such as receiving or sending data), the socket changes to synchronous. This means that the socket will not receive further asynchronous notifications before the synchronization operation is complete. Perform the following operations because of the socket switching mode:
CMySocket: OnReceive () {//... ar> str ;//...}
IfCSocketIf a two-state object is not implemented, you may receive additional notifications of similar events while processing the preceding notifications. For example
OnReceive
May receive
OnReceive
Notification. In the above code snippet, extract from the archive
Str
It may lead to recursion. By switching the status,CSocketUse methods to prevent additional notifications to prevent recursion. The general rule is that there is no notification in the notification.
The CHATTER and CHATSRVR sample applications explain this usage. For the source code and information of the MFC example, see the MFC example.
Note:
CSocketFileIt can also be used as
CArchiveObject (limited) file usage. By default,
CSocketFileConstructor
BArchiveCompatibleThe parameter is
TRUE. This specifies the object used for archiving. To use an archive object, go
BArchiveCompatiblePassing in Parameters
FALSE.
In "ARCHIVE compatibility" mode,CSocketFileObjects provide better performance and reduce the risk of "deadlock. A deadlock occurs when both the sending and receiving sockets are waiting for the other party or waiting for public resources. IfCArchiveObject ProcessingCFileObject ProcessingCSocketFile. ProcessingCFileIf the number of bytes received by the archive is smaller than that requested by the archive, the archive has reached the end of the file. ProcessingCSocketFileThe data is message-based, and the buffer zone can contain multiple messages. Therefore, the number of bytes received is smaller than the number of bytes requested. The application is not blocked in this case (insteadCFileIt can continue to read messages from the buffer until the buffer becomes empty. In this case,CArchiveThe IsBufferEmpty function in helps to monitor the status of the Archive Buffer.
For more information, see Windows Sockets: Use a socket with an archive.