UINT TSend(LPVOID pParam)
{
  CFileviewDlg* pDlg=(CFileviewDlg*)pParam;
  for (;;)
  {
    ::GetMessage(&(pDlg->ConSock.BufMsg),NULL,0,0);
    SendMessage(pDlg->m_hWnd,WM_ST_SEND,0,0);
    pDlg->ConSock.EventSend.Lock();
    char* buf=(char*)(pDlg->ConSock.BufMsg.lParam);
    delete buf;
  }
  return 0;
}void CFileviewDlg::OnButton1() 
{
  // TODO: Add your control notification handler code here
  int num=10240*3;
  num=1024;
  char* str=new char[num];
  ::PostThreadMessage(pTSend->m_nThreadID,0,num,LPARAM(str));
}void CFileviewDlg::OnSTSend()
{
  ConSock.HASMSG=TRUE;
  ConSock.AsyncSelect();
}class CConSock : public CAsyncSocket
{
// Attributes
public:
  CEvent EventSend;
  MSG BufMsg;
  char* pSData;
  int m_nBytesBufferSize;        //bufsize
  int m_nBytesSent;      //n_bytesent
  BOOL MSGSended;
  BOOL HASMSG;CConSock::CConSock()
{
  pSData=(char*)&BufMsg;
  m_nBytesBufferSize=sizeof(BufMsg);
  m_nBytesSent=0;
  MSGSended=FALSE;
  HASMSG=FALSE;
}void CConSock::OnSend(int nErrorCode) 
{
  // TODO: Add your specialized code here and/or call the base class
  if (HASMSG)
  {
    if (MSGSended==FALSE/*&&DATASended==FALSE*/)
    {
      
      while (m_nBytesSent<m_nBytesBufferSize)
      {
        int dwByte;
        if((dwByte=Send(pSData+m_nBytesSent,m_nBytesBufferSize-m_nBytesSent))==SOCKET_ERROR)
        {
          if (GetLastError() == WSAEWOULDBLOCK)
          {
            AsyncSelect(FD_WRITE);
            return;
          }
          else
          {
            TCHAR szError[256];
            wsprintf(szError, "Server Socket failed to send: %d", 
            GetLastError());
            Close();
            AfxMessageBox (szError);
          }
        }
        else
        {
          m_nBytesSent+=dwByte;
        }
      }
          if(m_nBytesSent==m_nBytesBufferSize)
          {
            MSGSended=TRUE;
            pSData=(char*)BufMsg.lParam;
            m_nBytesBufferSize=BufMsg.wParam;
            m_nBytesSent=0;
          }
    }
    if(MSGSended==TRUE/*&&DATASended==FALSE*/)
    {
      while (m_nBytesSent<m_nBytesBufferSize)
      {
        int dwByte;
        if((dwByte=Send(pSData+m_nBytesSent,m_nBytesBufferSize-m_nBytesSent))==SOCKET_ERROR)
        {
          if (GetLastError() == WSAEWOULDBLOCK)
          {
            AsyncSelect(FD_WRITE);
            return;
          }
          else
          {
            TCHAR szError[256];
            wsprintf(szError, "Server Socket failed to send: %d", 
            GetLastError());
            Close();
            AfxMessageBox (szError);
          }
        }
        else
        {
          m_nBytesSent+=dwByte;
        }
      }
        if(m_nBytesSent==m_nBytesBufferSize)
        {
          //DATASended=TRUE;
          pSData=pSData=(char*)&BufMsg;
          m_nBytesBufferSize=sizeof(BufMsg);
          m_nBytesSent=0;
          MSGSended=FALSE;
          //DATASended=FALSE;
          HASMSG=FALSE;
          //AfxMessageBox("ok");
          EventSend.SetEvent();
        }
    }
  }
  
  CAsyncSocket::OnSend(nErrorCode);
}

解决方案 »

  1.   

    直接原因: 自定义消息WM_ST_SEND的响应函数OnSTSend定义不对
    LRESULT OnSTSend(); ==>
    LRESULT OnSTSend(WPARAM wp, LPARAM lp);
      

  2.   

    自定义消息的处理函数,必须定义如下:afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);返回值必须是HRESULT型,否则Debug会过,而Release出错
      

  3.   

    DEBUG下,编译器会自动对你的某些变量和函数返回值进行初始化
    但是在RELEASE模式下,不会执行
    因此会出现问题