private void ReceiveThreadHandler(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
if (aReceivedCBHandler != null)
{
aReceivedCBHandler(tcpClient, message, bytesRead);
}
}
//Join the client thread
((Thread)RxTCPClientThread[TcpClients.IndexOf(client)]).Join() ;
//remove the thread from the thread arraylist
RxTCPClientThread.RemoveAt(TcpClients.IndexOf(client));
//remove the client from the client arraylist
TcpClients.Remove(client);
//notify the user application
if (aClientLostCBHandler != null)
{
aClientLostCBHandler(tcpClient);
}
//close the client socket.
tcpClient.Close();
}