Поделиться через


CAsyncSocket::OnReceive

Called by the framework to notify this socket that there is data in the buffer that can be retrieved by calling the Receive member function.

virtual void OnReceive(
   int nErrorCode 
);

Параметры

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

    • 0   The function executed successfully.

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

Заметки

For more information, see Windows Sockets: Socket Notifications.

Пример

void CMyAsyncSocket::OnReceive(int nErrorCode)   // CMyAsyncSocket is 
                                                // derived from CAsyncSocket
{
   static int i = 0;

   i++;

   TCHAR buff[4096];
   int nRead;
   nRead = Receive(buff, 4096); 

   switch (nRead)
   {
      case 0:
        Close();
        break;
      case SOCKET_ERROR:
        if (GetLastError() != WSAEWOULDBLOCK) 
        {
          AfxMessageBox (_T("Error occurred"));
          Close();
        }
        break;
      default:
        buff[nRead] = _T('\0'); //terminate the string
        CString szTemp(buff);
        m_strRecv += szTemp;   // m_strRecv is a CString declared 
                        // in CMyAsyncSocket
        if (szTemp.CompareNoCase(_T("bye")) == 0)
        {
           ShutDown();
           s_eventDone.SetEvent();
        }
   }
   CAsyncSocket::OnReceive(nErrorCode);
}

Требования

Header: afxsock.h

См. также

Основные понятия

CAsyncSocket Class

CAsyncSocket Members

Hierarchy Chart

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnConnect

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnSend

CAsyncSocket::Receive