I am reading about IOCP through this article
http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5i.html
This first WSARecv function he used with 0 lpNumberOfBytesRecvd
to initiate the completion ports
if (WSARecv(Accept, &(PerIoData->DataBuf), 1, &RecvBytes, &Flags, &(PerIoData->Overlapped), NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != ERROR_IO_PENDING)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
return 1;
}
}
else
printf("WSARecv() is OK!\n");
}
Now on the worker threads function where the application receives the IO post
it checks parameter lpNumberOfBytes
of GetQueuedCompletionStatus
for 0 also and then it closes the connection if it is
// First check to see if an error has occurred on the socket and if so
// then close the socket and cleanup the SOCKET_INFORMATION structure
// associated with the socket
if (BytesTransferred == 0)
When i tried it the first IOCP message posted the function GetQueuedCompletionStatus
it always returning 0 in lpNumberOfBytes
parameter because first WSARecv
call was called with lpNumberOfBytesRecvd
set to zero
I have tried to implement this code and i am having troubles to get the code working unless i choose to not use NULL in this parameter
in the first call to WSARecv
so that the GetQueuedCompletionStatus
function returning value 0 in lpNumberOfBytes
can be used as in case of all data recieved.
MSDN says to use NULL
lpNumberOfBytesRecvd [out]
A pointer to the number, in bytes, of data received by this call if the receive operation completes immediately.
Use NULL for this parameter if the lpOverlapped parameter is not NULL to avoid potentially erroneous results. This parameter can be NULL only if the lpOverlapped parameter is not NULL.
The code is not working well if i call WSARecv
again (inside worker threads function) and all data were recieved this causes the GetQueuedCompletionStatus
function to return immediatly without waiting even if INFINITE used and the worker threads enter in endless loop. No errors ever returned by GetQueuedCompletionStatus
function it just acts as if there is more data but there is none.
WSARecv
needs to be initiated with what value at buf size?
also should i use non blocking socket? or just normal blocking sockets since i use separate thread to accept connections so i think its fine because it won't freeze my GUI.