다음을 통해 공유


CAsyncSocket::OnSend

Called by the framework to notify the socket that it can now send data by calling the Send member function.

virtual void OnSend(
   int nErrorCode 
);

Parameters

  • nErrorCode
    The most recent error on a socket. The following error codes apply to the OnSend 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

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

Requirements

Header: afxsock.h

See Also

Reference

CAsyncSocket Class

Hierarchy Chart

CAsyncSocket::GetLastError

CAsyncSocket::OnAccept

CAsyncSocket::OnClose

CAsyncSocket::OnConnect

CAsyncSocket::OnOutOfBandData

CAsyncSocket::OnReceive

CAsyncSocket::Send

Other Resources

CAsyncSocket Members