CAsyncSocket::Receive()、OnReceive()__CAsyncSocket

來源:互聯網
上載者:User

virtual int Receive(   void* lpBuf,   int nBufLen,   int nFlags = 0 );
首先設定Socket感興趣的事件AsyncSelect(FD_READ)參數
,之後虛函數OnReceive()一直掃描,當掃描到要接收的資料時,調用Receive()接收資料。
lpBuf
輸入資料的緩衝區。
nBufLen
lpBuf  的位元組長度。
nFlags
指定調用了的方式。   通訊端選項和參數 nFlags 依賴於此函數語義。   後者將以下值中的任何一個構造與C++  OR 運算子 :
在詳細介紹資料的 MSG_PEEK 偷看。   該資料複製到緩衝區,但從輸入隊列不會被取消。

MSG_OOB 處理帶外資料。 傳回值

如果未發生錯誤,Receive 返回接收到的位元組數。 如果串連已關閉,則返回0。 否則,SOCKET_ERROR 的值返回,並且,特定錯誤碼可以通過調用 GetLastError檢索。 下面的錯誤適用於此成員函數:

WSANOTINITIALISED 的成功的 AfxSocketInit 必須在使用此API之前發生。

WSAENETDOWN Windows通訊端實現檢測網路子系統失敗。

WSAENOTCONN 通訊端未串連。

塊Windows通訊端操作的WSAEINPROGRESS 的進度中。

WSAENOTSOCK 描述符不是通訊端。

WSAEOPNOTSUPP     MSG_OOB 指定了,但是,通訊端不是類型 SOCK_STREAM

WSAESHUTDOWN 通訊端已關閉;,在 ShutDown 調用具有 nHow 設定為0或2.後,調用通訊端的 Receive 是不可能的。

WSAEWOULDBLOCK 通訊端標記為未佔用,並 Receive 操作將阻止。

WSAEMSGSIZE 中的資料進行太大而無法放入指定緩衝區和被截斷。

WSAEINVAL 通訊端尚未綁定與 Bind

WSAECONNABORTED 虛擬電路中止的是由於逾時或其他故障。

虛擬電路遠程重設WSAECONNRESET

virtual void OnReceive(    int nErrorCode  );
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);}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.