Saturday, May 16, 2009

Hello,

thank you for providing the winsock tutorials!
Verbose and easy to understand!

When experimenting with the overlapped model sample,
(http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5e.html)
an invalid parameter error occured with WSAWaitForMultipleEvents:
DWORD WINAPI ProcessIO(LPVOID lpParameter)
{
.....
if ((Index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE,
WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)

The invalid parameter was EventTotal == 0, and not == 1 as expected.
To fix the bug you must set EventTotal = 1 *before* you create the
thread for ProcessIO:


if ((EventArray[0] = WSACreateEvent()) == WSA_INVALID_EVENT)

{

printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());

return 1;

}

else

printf("WSACreateEvent() is OK!\n");

////////// ==> fix: set EventTotal = 1 here:
EventTotal = 1;


// Create a thread to service overlapped requests

if (CreateThread(NULL, 0, ProcessIO, NULL, 0, &ThreadId) == NULL)

{

printf("CreateThread() failed with error %d\n", GetLastError());

return 1;

}

else

printf("Nothing to say, CreateThread() is OK!\n");


////////// ==> too late here, ProcessIO() could run already:
// EventTotal = 1;

No comments:

Post a Comment