Original Post
Hi, I am developing a Winsock Server Application based off the WSAEventSelect() I/O model and I am having some trouble. The Server needs to be able to handle 500 connections, and because WSAWaitForMultipleEvents() can only wait on 64 objects I am forced to make my server multi-threaded (this is where the problem comes in). I have searched google, codeproject, ect.. for multi-threaded Winsock Servers based off WSAEventSelect() so I could get a somewhat basic idea on how people have done it and I have not found anything, only ideas on how it would be done. Still searching the forums here (have been for hours), here's a basic layout of my program's structure: I created a class called WsaSocketClass that contains the following
DWORD EventTotal;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS]; When the Application starts a thread is created that checks for new connections / socket data WSAWaitForMultipleEvents()
WSAEnumNetworkEvents()
WSAEventSelect(AcceptedSocket, ...)
this->CreateSockInfo(AcceptedSocket); // see below, this is called after a connection is recieved Once a connection is recieved typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
BOOL WsaSocketClass::CreateSockInfo(SOCKET s)
{
LPSOCKET_INFORMATION SI;
if ((this->EventArray[this->EventTotal] = WSACreateEvent()) == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
return FALSE;
}
if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("GlobalAlloc() failed with error %d\n", GetLastError());
return FALSE;
}
SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;
this->SocketArray[this->EventTotal] = SI;
this->EventTotal++;
return (TRUE) ;
} So this is where I am stuck and I do not know what to do.. I have tried creating a new thread once 63 Clients connect to the server and reading everything based off a new class, but it doesn't work because the old thread (the first one used when the Server started for the first 63 Clients) is still trying to accept connections and because of that I recieve errors that WSA_MAXIMUM_WAIT_EVENTS has been reached. I don't know how I would wait on more then one I/O notification (for receiving Client connections) if everything is based off a new class and the listen socket (FD_ACCEPT|FD_CLOSE) is already waiting on an I/O notification from the first thread on the same socket. Has anyone here developed (or know of) a multi-threaded Winsock Application based off the WSAEventSelect() I/O model that cares to share source? or any articles (books)? anything I could possibly learn off of? Any help is very much appreciated as I am clueless on how to go any further and am stressed, thanks in advance.