This article explains the operation sequence of the server socket and client socket in a comparative way. Because these sockets useCArchiveObjects, so they must be streaming sockets. Operation Sequence of stream socket communication
In constructionCSocketFileBefore the object, the following sequence pairsCAsyncSocketAndCSocketThey are all accurate (only a few parameters are different ). Slave ConstructionCSocketFileObject start. The order only appliesCSocket. The following table describes the operation sequence of communication between the client and the server.
Set the communication between the server and the client
Server |
Client |
// Construct a socket CSocket sockSrvr; |
// Construct a socket CSocket sockClient; |
// Create the SOCKET SockSrvr. Create (nPort); 1, 2 |
// Create the SOCKET SockClient. Create (); 2 |
// Start listening SockSrvr. Listen (); |
|
|
// Seek a connection SockClient. Connect (strAddr, nPort); 3, 4 |
// Construct a new, empty socket CSocket sockRecv; // Accept connection SockSrvr. Accept (sockRecv); 5 |
|
// Construct file object CSocketFile file (& sockRecv ); |
// Construct file object CSocketFile file (& sockClient ); |
// Construct an archive CArchive arIn (& file, CArchive: load ); -Or- CArchive arOut (& file, CArchive: store ); -Or both- |
// Construct an archive CArchive arIn (& file, CArchive: load ); -Or- CArchive arOut (& file, CArchive: store ); -Or both- |
// Use the archive to pass data: ArIn> dwValue; -Or- ArOut <dwValue; 6 |
// Use the archive to pass data: ArIn> dwValue; -Or- ArOut <dwValue; 6 |
1. Here'sNPortIs the port number. For port details, see Windows Sockets: Port and socket address.
2. The server must always specify a port so that the client can connect.CreateAn address is also specified for a call. Use default parameters on the client. These parameters require that MFC use any available port.
3. Here'sNPortYesport number,StrAddrIs the computer address or Internet Protocol (IP) address.
4. The computer address can be in the following forms: "ftp.microsoft.com" and "microsoft.com ". IP addresses are separated by numbers, for example, 127.54.67.32 ".ConnectFunction to check whether the address is a number separated by points (but it does not ensure that the number is a valid computer on the network ). If notConnectUse another form of computer name.
5. When called on the serverAcceptTo pass a reference to the new socket object. The object must be constructed first, but not called.Create. Note: If the socket object is out of the range, the connection is closed. MFC connects new objectsSOCKETHandle. This socket can be constructed on the stack (as shown in the table) or on the stack.
6. When the archive and socket files are out of range, they will be closed. When a socket object is out of the range or is deleted, the object's destructor also calls the Close member function for this socket object. Other Instructions on order
The call sequence shown in the preceding table applies to streaming sockets. The data PACKET socket is connectionless and does not require CAsyncSocket: Connect, Listen, or Accept (but can be used selectively ).Connect). Conversely, if you are usingCAsyncSocketClass, the data PACKET socket is usedCAsyncSocket: SendToAndReceiveFromMember functions. (If you useConnect, UseSendAndReceive.) BecauseCArchiveIt is not applicable to data packets. If the socket is a data packet, do not use an archivedCSocket.
CSocketFile is not supportedCFileAll functions,CFileMembers (suchSeek) Is meaningless for socket communication and is unavailable. Therefore, some default MFCSerializeFunctions andCSocketFileIncompatible. ForCEditViewClass. Do not try to useCEditView: SerializeRawBy attachingCSocketFileObjectCArchiveObject serializationCEditViewData, but should useCEditView: Serialize(No source ). The SerializeRaw function expects that the file object hasCSocketFileUnsupported functions, suchSeek.
For more information, see: Windows Sockets: Use a socket with an archive Windows Sockets: use Windows Sockets such as CAsyncSocket: Port and socket address Windows Sockets: streaming socket Windows Sockets: data PACKET socket