I am writing a server and I am probably blind to what I have done wrong. I want to limit the amount of connections to 10, but it is currently allowing 11. Not that this is a big deal, but I am confuzzled.
csck[0 - 9] are all initialized to INVALID_SOCKET
After I get the message "All sockets full ...", it allows one more connection, which doesn't result in any more messages to the console. After the 11th, any clients that try to connect get forcefully rejected, as desired.
I am designing on 32-bit Windows 7 Beta.
lsck is the listening socket, csck[] are the specific client sockets
Code:
while (lsck != INVALID_SOCKET)
{
if (full == 1)
{
printf("All sockets full, waiting 60 seconds for one or more to free up\n");
Sleep(60000);
}
for (i = 0; i < 10; i++)
{
if (csck[i] == INVALID_SOCKET)
{
printf("\tListening on socket %d\n", i);
csck[i] = accept(lsck, NULL, NULL);
if (csck[i] == INVALID_SOCKET)
{
closesocket(lsck);
printf("Error on Listening Socket ... Closing down\n");
break;
}
else
{
printf("\tClient connected\n");
/*
Make child threads and stuff
*/
break;
}
}
full = (i == 9) ? 1 : 0;
}
}
Edit: The sockets will be freed from inside the thread when the connection is terminated, but I haven't designed that part yet.