我在用异步传输数据时,参考了微软的例子:http://msdn.microsoft.com/zh-cn/library/wfttx1af.aspx
代码如下:
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);
}
看了看解释,shutdown是结束本次接收,但是能够继续接收数据。
但是我在代码中用shutdown时,接收数据n次后,就停止了,服务器端出现10053错误。
不用shutdown时反而没事,不知道为什么。