This is the first part of HandleClientComm()
:
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream stm = clientList[n].GetStream();
msg = new TheMessage();
You have the tcpClient
, which is the client you sent as a parameter, but the NetworkStream
is not for that client, but for clientList[n]
, and n
is a class-wide variable. Later in that method, within the while
loop, you use:
stm = clientList[n].GetStream();
As soon as you increase n
, all threads running HandleClientComm()
will receive and send messages from/to the last client.
The NetworkStream
you use in HandleClientComm()
should be created from the tpcClient
instead, so each thread running HandleClientComm()
serves its own client:
stm = theClient.GetStream();