DWORD WINAPI ServerWorkerThread(LPVOID lpParameter)
{
HANDLE ComplectionPort = (HANDLE)lpParameter;
PER_IO_OPERATION_DATA *PerIOData;
PER_HANDLE_DATA *PerHandleData;
DWORD BytesTransferred;
DWORD SendBytes;
DWORD RecvBytes;
while(TRUE)
{
GetQueuedCompletionStatus(ComplectionPort,&BytesTransferred,(LPDWORD)&PerHandleData,(LPOVERLAPPED*)&PerIOData,INFINITE);
if(BytesTransferred == 0)
{
printf("%d 连接关闭\n",PerHandleData->Socket);
closesocket(PerHandleData->Socket);
GlobalFree(PerHandleData);
GlobalFree(PerIOData);
continue;
}
switch(PerIOData->IOOperation)
{
case IORead:
{
//PerIOData->DATABuf.buf = PerIOData->DATABuf.buf;
PerIOData->DATABuf.len = BytesTransferred;
PerIOData->SendBytes = BytesTransferred;
PerIOData->IOOperation = IOWrite;
WSASend(PerHandleData->Socket,&PerIOData->DATABuf,1,&SendBytes,0,&PerIOData->OVerlapped,NULL);
break;
}
case IOWrite:
{
ZeroMemory(&PerIOData->OVerlapped,sizeof(PerIOData->OVerlapped));
PerIOData->DATABuf.buf = PerIOData->DATABuf.buf + PerIOData->SendBytes;
PerIOData->DATABuf.len -= PerIOData->SendBytes;
if (PerIOData->DATABuf.len != 0)
{
WSASend(PerHandleData->Socket,&PerIOData->DATABuf,1,&SendBytes,0,&PerIOData->OVerlapped,NULL);
break;
}
PerIOData->DATABuf.buf = PerIOData->Buffer;
PerIOData->DATABuf.len = sizeof(PerIOData->Buffer);
PerIOData->IOOperation = IORead;
DWORD Flags = 0;
WSARecv(PerHandleData->Socket,&PerIOData->DATABuf,1,&RecvBytes,&Flags,&PerIOData->OVerlapped,NULL);
}
default:
{
Sleep(1);
}
}
}
return 0;
}
这个是工作线程~~~
测试没有问题~~~模仿例子,自己改的~~
但是我觉得会存在一个问题~~
就是说,如果调用WSASend发送数据之后,有可能数据发送不完整,可能需要多次发送~~
如果发送了第一次之后,另外一个socket收到了数据,这个时候,就可能会覆盖到WSASend的数据,从而导致第二次的发送失败~~~
有可能存在这个问题吗?
应该如何修改~~~~・
请各位帮帮忙~
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)