Windows Sockets: Example of a socket with an archive

Source: Internet
Author: User

This article provides an example of using the CSocket class. This example usesCArchiveThe object uses a socket to serialize data. Note that this is not serialized from a file or to a file.

The following example illustrates how to use ArchiveCSocketThe method by which the object sends and receives data. This example is designed to exchange data between two instances of an application (on the same computer or on different computers on the network. One instance sends data, and the other instance receives and confirms the data. Each application can start the exchange or act as the server or client of another application. The following functions are defined in the View class of the application:

void CBlabberView::PacketSerialize(long nPackets, CArchive& arData, CArchive& arAck) { if (arData.IsStoring()) { CString strText; for(int p = 0; p < nPackets; p++) { BYTE bValue = (BYTE)(rand()%256); WORD nCopies = (WORD)(rand()%32000); // Send header information arData < < bValue < < nCopies; for(int c = 0; c < nCopies; c++) { // Send data arData < < bValue; } Text.Format("Received Packet %d of %d (Value=%d,Copies=%d)",p,nPackets,(int)bValue,nCopies); // Send receipt string arData < < strText; arData.Flush(); // Receive acknowledgment arAck >>strText; // display it DisplayMessage(strText); } } else { CString strText; BYTE bCheck; WORD nCopies; for(int p = 0; p < nPackets; p++) { // Receive header information arData >>bCheck >>nCopies; for(int c = 0; c < nCopies; c++) { // Receive data arData >>bValue; if (nCheck != bValue) AfxMessageBox("Packet Failure"); } } // Receive receipt string and display it arData >>strText; DisplayMessage(strText); Text.Format("Sent Packet %d of %d (Value=%d,Copies=%d)",p,nPackets,(int)bValue,nCopies); // Send acknowledgment arAck < < strText; arAck.Flush(); } }

The most important thing about this example is: its structure and MFCSerializeFunctions have similar structures.PacketSerializeMember functionElseClauseIfStatement. This function receives two CArchive references as parameters:ArDataAndArAck. IfArDataIf the archive object is set to storage (send ),IfBranch; otherwise, ifArDataSet to load (receive), and the function will executeElseBranch. For more information about serialization in MFC, see serialization.

Note:Assume that ArAckOperations and ArDataOn the contrary: ArDataUsed for sending, ArAckUsed for receiving, and the opposite is true.

For sending, this example function cyclically Based on the specified number of times, and each time it generates random data for demonstration purposes. The application obtains real data from a source (such as a file.ArDataInsert operators for archiving (<) Is used to send a stream composed of three consecutive data blocks:

  • Specifies the "Header" of the data.bValue Variable value and number of sent copies ).

    In this example, both items are randomly generated.

  • The number of data copies.

    InternalForSend cyclically by specified number of timesbValue .

  • What the acceptor displays to the user isstrText .

Function operations are similar in terms of receipt. The difference is that it uses the archive extract operator (>. Receive the data Received by the application verification, display the final Received message, and send back a message indicating that the Sent message is displayed for the Sent application.

In this communication model, the word "Received" (Received (strText Message sent in the variable) is displayed on the other end of the communication. It indicates to the receiving user that a certain number of packets have been received. The receiving end replies with a similar string indicating "Sent" (Sent), which is displayed on the screen of the original sending end. The receipt of the two strings indicates that the communication is successful.

WarningIf you are writing an MFC client program that communicates with an existing (non-MFC) server, do not send C ++ objects through archive. Unless the server is an MFC application that knows the type of the object to be sent, the server will not be able to receive or deserialize the object. Windows Sockets: the example in the byte sorting shows a communication of this type.

For more information, see the Windows Sockets specification:Htonl,Htons,Ntohl,Ntohs. For more information, see:

Windows Sockets: Derived from the socket class

Windows Sockets: working with an archived socket

Windows Sockets: Background

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.