So I am currently experementing with Sockets and I want to have 3 ports open for client sockets to connect to. So this works if I manually add my threads.
Thread t = new Thread(() => SyncServer.StartListening(new IPAddress(new byte[]{127,0,0,1}), port));
t.Start();
Thread t2 = new Thread(() => SyncServer.StartListening(new IPAddress(new byte[]{127,0,0,1}), port + 1));
t2.Start();
....
But I want to do it a bit cleaner so I tried with a for loop
List<Thread> threads = new List<Threads>();
for(int i = 0; i<3;i++)
{
Thread t = new Thread(() => SyncServer.StartListening(new IPAddress(new byte[]{127,0,0,1}), port + i));
t.Start();
threads.Add(t);
}
The socket server does fully work but when I work with threads and add them manually. If I work with a for loop, the socket server with the port becomes unavailable. Probably because the thread is stopped and I fail to understand why.
Note: SyncServer is a static class and StartListening is a static method. If I use a for loop, they always get the latest portnumber (in this case 11003).