Export (0) Print
Expand All

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.

NoteNote

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft