Share via


CAsyncSocket::OnConnect

Llamado por el marco para notificar este socket de conexión que el intento de conexión está completo, si correctamente o de error.

virtual void OnConnect(
   int nErrorCode 
);

Parámetros

  • nErrorCode
    el error más reciente en un socket.Los códigos de error siguientes se aplican a la función miembro de OnConnect :

    • Función de0 The ejecutada correctamente.

    • La dirección especificada speedWSAEADDRINUSE ya está en uso.

    • La dirección especificada speedWSAEADDRNOTAVAIL no está disponible en el equipo local.

    • Las direcciones deWSAEAFNOSUPPORT en la familia especificada no se pueden utilizar con este socket.

    • El intento deWSAECONNREFUSED speed conectarse fuertemente se han rechazado.

    • Se requiere la dirección de destino deWSAEDESTADDRREQ A.

    • el argumento deWSAEFAULT The lpSockAddrLen es incorrecto.

    • El socket deWSAEINVAL The ya está enlazado a una dirección.

    • El socket deWSAEISCONN The ya está conectado.

    • Los descriptores de archivo deWSAEMFILE No Más están disponibles.

    • Red deWSAENETUNREACH no se puede obtener acceso desde este host en este momento.

    • WSAENOBUFS ningún espacio en búfer está disponible.El socket no puede estar en línea.

    • El socket deWSAENOTCONN no está conectado.

    • WSAENOTSOCK El descriptor es un archivo, no un socket.

    • Intento deWSAETIMEDOUT speed conectarse agotó el tiempo de espera sin el establecimiento de una conexión.

Comentarios

[!NOTA]

En CSocket, la función de notificación de OnConnect nunca se llama.Para las conexiones, se llama simplemente Conectar, que cambiarán cuando se completa la conexión (correctamente o por error).Cómo se administran las notificaciones de conexión es un detalle de implementación de MFC.

Para obtener más información, vea Windows Sockets: Notificaciones de socket.

Ejemplo

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

Requisitos

encabezado: afxsock.h

Vea también

Referencia

Clase de CAsyncSocket

Gráfico de jerarquía

CAsyncSocket::Connect

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnReceive

CAsyncSocket::OnSend