CAsyncSocket::OnConnect

virtual void OnConnect( int nErrorCode );

Parameters

nErrorCode

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

Remarks

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

Important   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 the article Windows Sockets: Socket Notifications in Visual C++ Programmer's Guide.

Example

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);
}

CAsyncSocket OverviewClass MembersHierarchy Chart

See Also   CAsyncSocket::Connect, CAsyncSocket::GetLastError, CAsyncSocket::OnAccept, CAsyncSocket::OnClose, CAsyncSocket::OnOutOfBandData, CAsyncSocket::OnReceive, CAsyncSocket::OnSend