Windows Sockets: Use a socket with an archive

Source: Internet
Author: User

This article describes the CSocket programming model. The CSocket class provides more socket support than the CAsyncSocket class abstraction level.CSocketA version of the MFC serialization protocol is used to transmit data to or from a socket object through the MFC CArchive object.CSocketProvides blocking (managing background processing of Windows messages at the same time) and gives you accessCArchiveAndCArchiveYou must use the original API orCAsyncSocketClass to manage many aspects of communication.

Tip:Can be used separately CSocketClass CAsyncSocketBut the simplest programming model is to use CArchiveObject CSocket.

For more information about the working mechanism of socket implementation with archive, see Windows Sockets: Working Method of socket with archive. For sample code, see Windows Sockets: Operation Sequence and Windows Sockets: Socket example with archive. For information about some functions obtained by deriving your own class from the socket class, see Windows Sockets: deriving from the socket class.

Note:If you are writing an MFC client program that communicates with an existing (non-MFC) server, do not send the C ++ object 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 these objects. For more information about topics that communicate with non-MFC applications, see Windows Sockets: byte sorting.
CSocket Programming Model

UseCSocketObjects involve creating multiple MFC class objects and associating them. In the following general process, the server socket and client socket take each step (except step 3, each socket type in this step requires different operations ).

Tip:At runtime, the server application is usually prepared first and then "listens" to the client application when to seek a connection. If the server is not ready when the client tries to connect, the user application needs to try again later.

Set the communication between the server socket and the client socket

  1. Construct a CSocket object.
  2. Use this object to create a baseSOCKETHandle.

    ForCSocketClient object. Unless data packets are required for sockets, the default parameter should be used to Create the object. ForCSocketThe server object must beCreateThe specified port in the call.

    Note: CArchiveIt is not applicable to data packet sockets. If you want CSocketUsed for data PACKET socket, must be used like CAsyncSocketThis class is used without archiving. Because data packets are unreliable (delivery is not guaranteed and may be duplicated or in wrong order), they cannot be compatible with serialization through archiving. You expect the serialization operation to be completed reliably and sequentially. If you try CArchiveObject CSocketWhen used for data reporting, the MFC asserted failure.
  3. If the socket is a client object, call CAsyncSocket: Connect to Connect the socket object to the server socket.

    -Or-

    If the socket is a server object, call CAsyncSocket: Listen to start listening for connection attempts from the client. When a connection request is received, CAsyncSocket: Accept is called to Accept the request.

    Note: AcceptThe member function uses the new null CSocketObject Reference as its parameter. Before calling AcceptThe object must be constructed before. If the socket object is out of the range, the connection is closed. Do not call this new socket object Create.
  4. Create a CSocketFile objectCSocketThe object is associated with it.
  5. Create a CArchive object to load (receive) or store (send) data. This archive correspondsCSocketFileObject Association.

    Note:CArchiveIt is not applicable to data packet sockets.

  6. UseCArchiveThe object transmits data between the client socket and the server socket.

    Note: whether it is loading (receiving) or storing (sending), the givenCArchiveThe object only moves data in one direction. In some cases, twoCArchiveObject: one for sending data and one for receiving confirmation.

    After accepting the connection and setting the archive, you can perform tasks such as password verification.

  7. Destroys archives, socket files, and socket objects.
    Note: CArchiveClass provides CSocketClass used together IsBufferEmptyMember functions. For example, if the buffer zone contains multiple data messages, you need to cycle until all messages are read and the buffer zone is cleared. Otherwise, the next notification indicating that data is to be received may be delayed indefinitely. Use IsBufferEmptyMake sure that all data is retrieved. Usage IsBufferEmptySee the CHATSRVR sample application. For the source code and information of the MFC example, see the MFC example.

Windows Sockets: the operating sequence explains the two ends of the process with sample code.

For more information, see:

  • Windows Sockets: streaming socket
  • Windows Sockets: Data PACKET socket

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.