I have a server set up to listen on a port for clients, then if they find one add them to the client array and listen to them from another thread there.
Console.Write("Max Players: ");
maxPlayers = Int32.Parse(Console.ReadLine());
clients = new TcpClient[maxPlayers];
playerCount = 0;
formatter = new BinaryFormatter();
server = new TcpListener(IPAddress.Parse("192.168.1.33"), 7777);
server.Start();
while (true)
{
if (server.Pending() && playerCount < maxPlayers)
{
Console.WriteLine("Found client");
clients[playerCount] = server.AcceptTcpClient(); // Get client connection
//When one player joins, this should start a thread with an a playerCount of 0
Thread t = new Thread(() => ListenClient(playerCount));
t.Start();
playerCount++;
}
}
public static void ListenClient(int index)
{
while (true)
{
NetworkStream stream = clients[index].GetStream();
object obj = formatter.Deserialize(stream);
if (obj != null)
{
Console.WriteLine(obj);
}
}
}
However, when one player joins, the thread is called and passed an argument of 1, not 0 for some reason. What is the issue here?