重叠模型中WSARecv方法调用为何出错?
小弟做的重叠I/O模型的服务端代码如下:WSABUF buf;
WSAOVERLAPPED overlapped;
OVERLAPPED over;
int eventTotal=0;
WSAEVENT events[WSA_MAXIMUM_WAIT_EVENTS];
char mess[DATA_BUF];
SOCKET client;
DWORD recvBufLen;
DWORD dwFlags;
SOCKET server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in serAddr;
serAddr.sin_family=AF_INET;
serAddr.sin_port=htons(9050);
serAddr.sin_addr.s_addr=INADDR_ANY;
bind(server,(sockaddr*)&serAddr,sizeof(serAddr));
listen(server,5);
sockaddr_in cltAddr;
int addrLen=sizeof(cltAddr);
client=accept(server,(LPSOCKADDR)&cltAddr,&addrLen);
printf("接受到一个新的连接:%s,%d",inet_ntoa(cltAddr.sin_addr),ntohs(cltAddr.sin_port));
ZeroMemory(&buf,sizeof(buf));
buf.buf=mess;
buf.len=DATA_BUF;
ZeroMemory(&overlapped,sizeof(overlapped));
overlapped.hEvent=WSACreateEvent();
events[eventTotal]=overlapped.hEvent;
eventTotal++;
if(WSARecv(client,&buf,1,&recvBufLen,&dwFlags,&overlapped,NULL)==SOCKET_ERROR)
{
if(WSAGetLastError()!=WSA_IO_PENDING)
{
printf("recv client sending failed\n");
printf("%d",WSAGetLastError());
/*closesocket(client);
WSACloseEvent(events[0]);
return;*/
}
}
while (true)
{
int index;
index=WSAWaitForMultipleEvents(1,&events[0],TRUE,WSA_INFINITE,FALSE);
index=index-WSA_WAIT_EVENT_0;
WSAResetEvent(events[index]);
WSAGetOverlappedResult(client,&overlapped,&recvBufLen,FALSE,&dwFlags);
if(recvBufLen==0)
{
printf("client close the connection\n");
closesocket(client);
WSACloseEvent(events[index]);
break;
}
printf("接收到数据:%s",buf.buf);
。。。
}
closesocket(server);
当客户端连接上后,在语句“if(WSARecv(client,&buf,1,&recvBufLen,&dwFlags,&overlapped,NULL)==SOCKET_ERROR)
”出错,出错代码是10045,怎么回事呀,而且后面的WSAWaitForMultipleEvents方法也无法返回了?