다음을 통해 공유


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

Parameters

  • 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.

Remarks

For more information, see Windows Sockets: Socket Notifications.

Example

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

Requirements

Header: afxsock.h

See Also

Reference

CAsyncSocket Class

Hierarchy Chart

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnConnect

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnSend

CAsyncSocket::Receive

Other Resources

CAsyncSocket Members