Share via


CAsyncSocket::Accept

Call this member function to accept a connection on a socket.

virtual BOOL Accept(
   CAsyncSocket& rConnectedSocket,
   SOCKADDR* lpSockAddr = NULL,
   int* lpSockAddrLen = NULL 
);

Parameters

  • rConnectedSocket
    A reference identifying a new socket that is available for connection.

  • lpSockAddr
    A pointer to a SOCKADDR structure that receives the address of the connecting socket, as known on the network. The exact format of the lpSockAddr argument is determined by the address family established when the socket was created. If lpSockAddr and/or lpSockAddrLen are equal to NULL, then no information about the remote address of the accepted socket is returned.

  • lpSockAddrLen
    A pointer to the length of the address in lpSockAddr in bytes. The lpSockAddrLen is a value-result parameter: it should initially contain the amount of space pointed to by lpSockAddr; on return it will contain the actual length (in bytes) of the address returned.

Return Value

Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

  • WSANOTINITIALISED   A successful AfxSocketInit must occur before using this API.

  • WSAENETDOWN   The Windows Sockets implementation detected that the network subsystem failed.

  • WSAEFAULT   The lpSockAddrLen argument is too small (less than the size of a SOCKADDR structure).

  • WSAEINPROGRESS   A blocking Windows Sockets call is in progress.

  • WSAEINVAL   Listen was not invoked prior to accept.

  • WSAEMFILE   The queue is empty upon entry to accept and there are no descriptors available.

  • WSAENOBUFS   No buffer space is available.

  • WSAENOTSOCK   The descriptor is not a socket.

  • WSAEOPNOTSUPP   The referenced socket is not a type that supports connection-oriented service.

  • WSAEWOULDBLOCK   The socket is marked as nonblocking and no connections are present to be accepted.

Remarks

This routine extracts the first connection in the queue of pending connections, creates a new socket with the same properties as this socket, and attaches it to rConnectedSocket. If no pending connections are present on the queue, Accept returns zero and GetLastError returns an error. The accepted socket (rConnectedSocket) cannot be used to accept more connections. The original socket remains open and listening.

The argument lpSockAddr is a result parameter that is filled in with the address of the connecting socket, as known to the communications layer. Accept is used with connection-based socket types such as SOCK_STREAM.

Requirements

Header: afxsock.h

See Also

Reference

CAsyncSocket Class

Hierarchy Chart

CAsyncSocket::Bind

CAsyncSocket::Connect

CAsyncSocket::Listen

CAsyncSocket::Create

WSAAsyncSelect

Other Resources

CAsyncSocket Members