This part of the code gives a client that connects to the server an ID by checking if the ID is been used or not if it has been then check the next highest ID. For some reason this is causing a never ending loop why is that?
int IDcounter = 0;
bool IDfound = false;
bool IDused = false;
while (!IDfound)
{
Console.WriteLine("Current ID check " + IDcounter);
Console.WriteLine("Number of clients " + clientList.Count);
foreach (Client id in clientList)
{
if (id.ID == IDcounter)
{
IDused = true;
IDcounter++;
break;
}
}
if (!IDused)
{
client.ID = IDcounter;
IDfound = true;
}
}
clientList.Add(client);
You need to add an
elseto the finalifstatement:Consider what happens when you find that the first ID you check is taken by some client. You set
IDUsedtotrue, and thenbreakthe loop. Theif (!IDUsed)check is false (since the ID was used,) and so you repeat. But notice that you’ve never setIDUsedback tofalse. And in fact, there’s nowhere in the loop that happens, and so you loop forever.