CAsyncSocket的资料好象很少啊,大部分都是用WINDOWS API编程的
1.请问下在用CAsyncSocket中,怎么才能接收到服务器的数据,也就是怎么才会触发CAsyncSocket中的Receive()函数?我原来用串口来了个消息好象要消息触发,
LONG CTest1Dlg::OnCommunication(WPARAM ch, LPARAM port)
{
........
........
}
怎么在CAsyncSocket似乎没这样的用法
2.我看Receive(Buffer,sizeof(Buffer),0)使用中先要自己确定接收数据量Buffer的大小,分配一个数组空间,那我怎么知道要来多少数据呢?然后在分配空间?
谢谢大家了

解决方案 »

  1.   

    本来就怎么要用CAsyncSocket API也很简单你可以重载OnReceive()函数 当有数据可收会触发这个函数 msdn上的例子:
    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);
    }
      

  2.   

    找个完整的 CAsyncSocket 的例子程序看看就明白了
      

  3.   

    读不完数据会再次触发onreceive的...
      

  4.   

    第1:重载虚函数OnReceive ,在里面写你代码就好
    第2:缓冲区比你最大的包大就可以了,没什么限定. 数据包都是接收完后memset再重新接收
      

  5.   

    CSocket和CAsyncSocket都不是很好的socket封装,还是推荐用api最好。