I have a scenario in which i have one TcpListener
that can accept multiple TcpClient
(s); after accepting, they exchange datas in full duplex mode with NetworkStream
, because as defined in MS doc:
Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.
so, if I write a multithreaded implementation of acceptTcpListener like suggested in this question:
while (listening)
{
TcpClient client = listener.AcceptTcpClient();
// Start a thread to handle this client...
new Thread(() => HandleClient(client)).Start();
}
how can i stop gracefully one connection? Have I the possibility to "control" these threads i created?? I have for each thread that performs "HandleClient" one thread for read the NetworkStream and ondemand a thread that write over the NetworkStream.