CAsyncSocket::OnReceive

调用由框架发现此套接字在可通过调用 Receive 成员函数检索缓冲区的数据。

virtual void OnReceive(
   int nErrorCode 
);

参数

  • nErrorCode
    在套接字的最新错误。 以下错误代码应用于 OnReceive 成员函数:

    • 0 成功执行的函数。

    • WSAENETDOWN Windows套接字实现检测网络子系统失败。

备注

有关更多信息,请参见 Windows套接字:存储通知

示例

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选件类

层次结构图

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnConnect

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnSend

CAsyncSocket::Receive