共用方式為


CAsyncSocket::OnSend

呼叫以告知通訊端架構也可以透過呼叫 傳送 成員函式上傳送資料。

virtual void OnSend(
   int nErrorCode 
);

參數

  • nErrorCode
    在通訊端上最近的錯誤。下列程式碼錯誤適用於 OnSend 成員函式:

    • 0 成功執行的函式。

    • WSAENETDOWN 視窗通訊端實作偵測網路子系統失敗。

備註

如需詳細資訊,請參閱 Windows Sockets:通訊端告知

範例

// CMyAsyncSocket is derived from CAsyncSocket and defines the 
// following variables:
//    CString  m_sendBuffer;   //for async send
//    int      m_nBytesSent;
//    int      m_nBytesBufferSize;
void CMyAsyncSocket::OnSend(int nErrorCode)
{
   while (m_nBytesSent < m_nBytesBufferSize)
   {
      int dwBytes;

      if ((dwBytes = Send((LPCTSTR)m_sendBuffer + m_nBytesSent, 
         m_nBytesBufferSize - m_nBytesSent)) == SOCKET_ERROR)
      {
         if (GetLastError() == WSAEWOULDBLOCK)
       {
          break;
       }
         else
         {
            TCHAR szError[256];
            _stprintf_s(szError, _T("Server Socket failed to send: %d"), 
               GetLastError());
            Close();
            AfxMessageBox (szError);
         }
      }
      else
      {
         m_nBytesSent += dwBytes;
      }
   }

   if (m_nBytesSent == m_nBytesBufferSize)
   {
      m_nBytesSent = m_nBytesBufferSize = 0;
      m_sendBuffer = _T("");
   }

   CAsyncSocket::OnSend(nErrorCode);
}

需求

Header: afxsock.h

請參閱

參考

CAsyncSocket 類別

階層架構圖

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnConnect

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnReceive

CAsyncSocket::Send