Compartilhar via


CAsyncSocket::OnConnect

Called by the framework to notify this connecting socket that its connection attempt is completed, whether successfully or in error.

virtual void OnConnect( 
   int nErrorCode  
);

Parameters

  • nErrorCode
    The most recent error on a socket. The following error codes apply to the OnConnect member function:

    • 0   The function executed successfully.

    • WSAEADDRINUSE   The specified address is already in use.

    • WSAEADDRNOTAVAIL   The specified address is not available from the local machine.

    • WSAEAFNOSUPPORT   Addresses in the specified family cannot be used with this socket.

    • WSAECONNREFUSED   The attempt to connect was forcefully rejected.

    • WSAEDESTADDRREQ   A destination address is required.

    • WSAEFAULT   The lpSockAddrLen argument is incorrect.

    • WSAEINVAL   The socket is already bound to an address.

    • WSAEISCONN    The socket is already connected.

    • WSAEMFILE   No more file descriptors are available.

    • WSAENETUNREACH   The network cannot be reached from this host at this time.

    • WSAENOBUFS   No buffer space is available. The socket cannot be connected.

    • WSAENOTCONN   The socket is not connected.

    • WSAENOTSOCK   The descriptor is a file, not a socket.

    • WSAETIMEDOUT   The attempt to connect timed out without establishing a connection.

Remarks

Note

In CSocket, the OnConnect notification function is never called. For connections, you simply call Connect, which will return when the connection is completed (either successfully or in error). How connection notifications are handled is an MFC implementation detail.

For more information, see Windows Sockets: Socket Notifications.

Example

void CMyAsyncSocket::OnConnect(int nErrorCode)   // CMyAsyncSocket is  
                                                 // derived from CAsyncSocket
{
   if (0 != nErrorCode)
   {
      switch(nErrorCode)
      {
         case WSAEADDRINUSE: 
            AfxMessageBox(_T("The specified address is already in use.\n"));
            break;
         case WSAEADDRNOTAVAIL: 
            AfxMessageBox(_T("The specified address is not available from ")
            _T("the local machine.\n"));
            break;
         case WSAEAFNOSUPPORT: 
            AfxMessageBox(_T("Addresses in the specified family cannot be ")
            _T("used with this socket.\n"));
            break;
         case WSAECONNREFUSED: 
            AfxMessageBox(_T("The attempt to connect was forcefully rejected.\n"));
            break;
         case WSAEDESTADDRREQ: 
            AfxMessageBox(_T("A destination address is required.\n"));
            break;
         case WSAEFAULT: 
            AfxMessageBox(_T("The lpSockAddrLen argument is incorrect.\n"));
            break;
         case WSAEINVAL: 
            AfxMessageBox(_T("The socket is already bound to an address.\n"));
            break;
         case WSAEISCONN: 
            AfxMessageBox(_T("The socket is already connected.\n"));
            break;
         case WSAEMFILE: 
            AfxMessageBox(_T("No more file descriptors are available.\n"));
            break;
         case WSAENETUNREACH: 
            AfxMessageBox(_T("The network cannot be reached from this host ")
            _T("at this time.\n"));
            break;
         case WSAENOBUFS: 
            AfxMessageBox(_T("No buffer space is available. The socket ")
               _T("cannot be connected.\n"));
            break;
         case WSAENOTCONN: 
            AfxMessageBox(_T("The socket is not connected.\n"));
            break;
         case WSAENOTSOCK: 
            AfxMessageBox(_T("The descriptor is a file, not a socket.\n"));
            break;
         case WSAETIMEDOUT: 
            AfxMessageBox(_T("The attempt to connect timed out without ")
               _T("establishing a connection. \n"));
            break;
         default:
            TCHAR szError[256];
            _stprintf_s(szError, _T("OnConnect error: %d"), nErrorCode);
            AfxMessageBox(szError);
            break;
      }
      AfxMessageBox(_T("Please close the application"));
   }
   CAsyncSocket::OnConnect(nErrorCode);
}

Requirements

Header: afxsock.h

See Also

Reference

CAsyncSocket Class

Hierarchy Chart

CAsyncSocket::Connect

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnReceive

CAsyncSocket::OnSend