共用方式為


CAsyncSocket Class

Represents a Windows Socket — an endpoint of network communication.

class CAsyncSocket : public CObject

Members

Public Constructors

Name

Description

CAsyncSocket::CAsyncSocket

Constructs a CAsyncSocket object.

Public Methods

Name

Description

CAsyncSocket::Accept

Accepts a connection on the socket.

CAsyncSocket::AsyncSelect

Requests event notification for the socket.

CAsyncSocket::Attach

Attaches a socket handle to a CAsyncSocket object.

CAsyncSocket::Bind

Associates a local address with the socket.

CAsyncSocket::Close

Closes the socket.

CAsyncSocket::Connect

Establishes a connection to a peer socket.

CAsyncSocket::Create

Creates a socket.

CAsyncSocket::Detach

Detaches a socket handle from a CAsyncSocket object.

CAsyncSocket::FromHandle

Returns a pointer to a CAsyncSocket object, given a socket handle.

CAsyncSocket::GetLastError

Gets the error status for the last operation that failed.

CAsyncSocket::GetPeerName

Gets the address of the peer socket to which the socket is connected.

CAsyncSocket::GetPeerNameEx

Gets the address of the peer socket to which the socket is connected (handles IPv6 addresses).

CAsyncSocket::GetSockName

Gets the local name for a socket.

CAsyncSocket::GetSockNameEx

Gets the local name for a socket (handles IPv6 addresses).

CAsyncSocket::GetSockOpt

Retrieves a socket option.

CAsyncSocket::IOCtl

Controls the mode of the socket.

CAsyncSocket::Listen

Establishes a socket to listen for incoming connection requests.

CAsyncSocket::Receive

Receives data from the socket.

CAsyncSocket::ReceiveFrom

Receives a datagram and stores the source address.

CAsyncSocket::ReceiveFromEx

Receives a datagram and stores the source address (handles IPv6 addresses).

CAsyncSocket::Send

Sends data to a connected socket.

CAsyncSocket::SendTo

Sends data to a specific destination.

CAsyncSocket::SendToEx

Sends data to a specific destination (handles IPv6 addresses).

CAsyncSocket::SetSockOpt

Sets a socket option.

CAsyncSocket::ShutDown

Disables Send and/or Receive calls on the socket.

CASyncSocket::Socket

Allocates a socket handle.

Protected Methods

Name

Description

CAsyncSocket::OnAccept

Notifies a listening socket that it can accept pending connection requests by calling Accept.

CAsyncSocket::OnClose

Notifies a socket that the socket connected to it has closed.

CAsyncSocket::OnConnect

Notifies a connecting socket that the connection attempt is complete, whether successfully or in error.

CAsyncSocket::OnOutOfBandData

Notifies a receiving socket that there is out-of-band data to be read on the socket, usually an urgent message.

CAsyncSocket::OnReceive

Notifies a listening socket that there is data to be retrieved by calling Receive.

CAsyncSocket::OnSend

Notifies a socket that it can send data by calling Send.

Public Operators

Name

Description

CAsyncSocket::operator =

Assigns a new value to a CAsyncSocket object.

CAsyncSocket::operator SOCKET

Use this operator to retrieve the SOCKET handle of the CAsyncSocket object.

Public Data Members

Name

Description

CAsyncSocket::m_hSocket

Indicates the SOCKET handle attached to this CAsyncSocket object.

Remarks

Class CAsyncSocket encapsulates the Windows Socket Functions API, providing an object-oriented abstraction for programmers who want to use Windows Sockets in conjunction with MFC.

This class is based on the assumption that you understand network communications. You are responsible for handling blocking, byte-order differences, and conversions between Unicode and multibyte character set (MBCS) strings. If you want a more convenient interface that manages these issues for you, see class CSocket.

To use a CAsyncSocket object, call its constructor, then call the Create function to create the underlying socket handle (type SOCKET), except on accepted sockets. For a server socket call the Listen member function, and for a client socket call the Connect member function. The server socket should call the Accept function upon receiving a connection request. Use the remaining CAsyncSocket functions to carry out communications between sockets. Upon completion, destroy the CAsyncSocket object if it was created on the heap; the destructor automatically calls the Close function. The SOCKET data type is described in the article Windows Sockets: Background.

Note

When using MFC sockets in secondary threads in a statically linked MFC application, you must call AfxSocketInit in each thread that uses sockets to initialize the socket libraries. By default, AfxSocketInit is called only in the primary thread.

For more information, see Windows Sockets: Using Class CAsyncSocket and related articles., as well as Windows Sockets 2 API.

Inheritance Hierarchy

CObject

CAsyncSocket

Requirements

Header: afxsock.h

See Also

Reference

CObject Class

Hierarchy Chart

CSocket Class

CSocketFile Class