Windows Sockets: CAsyncSocket class

Source: Internet
Author: User

This section describes the usage of the CAsyncSocket class. Note that this class encapsulates the Windows Sockets API at a very low level.CAsyncSocketSuitable for programmers who know network communication details but want to Use callback to conveniently notify network events. Based on this assumption, this article only provides basic instructions. If you want to use Windows Sockets to easily process multiple network protocols in the MFC application, and do not want to give up flexibility, you can consider usingCAsyncSocket. You may also feel that writing a communication program is better than using it.CSocketThe general replacement model of the class is better.

In "MFC reference ",CAsyncSocket. Visual C ++ also provides the Windows Sockets specification in the Platform SDK. You can decide the details. Visual C ++ is not availableCAsyncSocket.

If you are not familiar with network communication and want to obtain a simple solution, useCArchiveThe CSocket class of the object. For more information, see Windows Sockets: Use a socket with an archive.

This article includes:

  • Create and useCAsyncSocketObject.
  • Your CAsyncSocket liability.
Create and use a CAsyncSocket object

Use CAsyncSocket

  1. Construct a CAsyncSocket object and use this object to create a baseSOCKETHandle.

    The establishment of sockets follows the two-phase constructed MFC mode.

    For example:

    CAsyncSocket sock; sock. Create (); // Use the default parameters
    -Or-
    CAsyncSocket * pSocket = new CAsyncSocket; int nPort = 27; pSocket-> Create (nPort, SOCK_DGRAM );

    The first constructor above creates one on the stack.CAsyncSocketObject. The second constructor is created on the stack.CAsyncSocket. The first Create call above uses the default parameter to Create a streaming socket, and the secondCreateCall to create a data PACKET socket with the specified port and address. (AnyCreateVersions can be used with any constructor .)

    CreateParameters include:

    • "Port": Short integer.

      For server sockets, the port must be specified. For client Sockets, the default value of this parameter is generally accepted. This value allows Windows Sockets to select a port.

    • Socket Type:SOCK_STREAM(Default) orSOCK_DGRAM.
    • Socket "Address", such as "ftp.microsoft.com" or "128.56.22.8 ".

      This address is the Internet Protocol (IP) address on the network. It is likely to always rely on the default value of this parameter.

    For explanations of the terms "Port" and "socket address", see Windows Sockets: Port and socket address.

  2. If the socket is a client, use CAsyncSocket: Connect to Connect the socket object to the server socket.

    -Or-

    If the socket is a server, set the socket to start listening (use CAsyncSocket: Listen) for connection attempts from the client. When receiving a connection request, use CAsyncSocket: Accept to Accept the request.

    After accepting the connection, you can perform password verification and other tasks.

    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. For examples, see Windows Sockets: Operation Sequence.
  3. By callingCAsyncSocketThe member functions of the Windows Sockets API function encapsulated by the object to communicate with other Sockets.

    See the Windows Sockets specification and CAsyncSocket class in "MFC reference.

  4. DestroyCAsyncSocketObject.

    If a socket object is created on the stack, the destructor of this object will be called when the contained function is out of the range. If you useNewYou must useDeleteOperator to destroy this object.

    The Destructor calls the Close member function of the object before destroying the object.

Examples of the order in the Code (actuallyCSocketObject), see Windows Sockets: Operation Sequence. Your responsibilities for CAsyncSocket

After the CAsyncSocket class object is created, this object encapsulates WindowsSOCKETHandle. UseCAsyncSocketIf you use the API directly, you must handle all problems that may be encountered. For example:

  • Blocking solution.
  • The byte order between the sending and receiving computers is different.
  • Converts Unicode to a multi-byte character set (MBCS) string.

For definitions and other information about these terms, see Windows Sockets: blocking, Windows Sockets: byte sorting, and Windows Sockets: conversion strings.

Despite these problems, if the application requires you to gain all the flexibility and control capabilities,CAsycnSocketClass may be the correct choice. If the application does not have this requirement, consider usingCSocketClass.CSocketHides a large amount of details from you: it extracts Windows messages during blocking calls and gives you accessCArchiveAndCArchiveManages byte order differences and String Conversion for you.

For more information, see:

    Windows Sockets: Background

    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.