I am creating a multithreaded server which accepts multiple users and puts them in either a game or spectator view. I am having a hard time trying to get my head round how to make it so when clients join they can join the game until the max clients has reached. Then when the max clients has been reached they get put into a spectator queue until another clients leaves.
This is what I have for my server code
`
int callThread(SOCKET ClientSocket, int ID)
{
ClientCount++;
char recvbuf[DEFAULT_BUFLEN];
char *join = "joining";
char *spec = "spectator";
int recvbuflen = DEFAULT_BUFLEN;
int iResult, iSendResult;
// Receive until the peer shuts down the connection
printf("thread count: %d\n", ClientCount);
do {
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
//check if clientID is in queue if it is send confirmation
for (int i = 0; i < gameQueue.size(); i++)
{
if (gameQueue[i] = ID)
{
send(ClientSocket, join, strlen(join), 0);
}
}
//check if game is empty if not add ID to queue
if (gameQueue.size() < 5)
{
gameQueue.push(ID);
}
// check if game is full if it is add ID to spectator queue
if (gameQueue.size() == 5)
{
specQueue.push(ID);
}
if (iResult > 0) {
printf("Bytes received: %d\n", iResult);
for (int i = 0; i < iResult ; i++)
{
cout << recvbuf[i];
}
// Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
MyMutex.lock();
ClientCount--;
MyMutex.unlock();
closesocket(ClientSocket); WSACleanup();
return 1;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
{
MyMutex.lock();
ClientCount--;
MyMutex.unlock();
closesocket(ClientSocket);
printf("Connection closing...\n");
}
else {
printf("recv failed: %d\n", WSAGetLastError());
MyMutex.lock();
ClientCount--;
MyMutex.unlock();
closesocket(ClientSocket);
return 1;
}
if (recvbuf[0] == 'y')
{
MyMutex.lock();
cout << "Client" << ID << "has quit" << endl;
ClientCount--;
MyMutex.unlock();
closesocket(ClientSocket);
iResult = 0;
}
} while (iResult > 0);
}
`
This is my thread I can post the rest of my code when requested
It looks like you're new here. If you want to get involved, click one of these buttons!