2

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.

Community
  • 1
  • 1
Alexander.It
  • 187
  • 1
  • 1
  • 16
  • 2
    Have you tried just closing the `NetworkStream`? I use `Socket` for my network I/O so I'm less familiar with `TcpClient`. But given it lacks a `Shutdown()` method, I would assume that it and `NetworkStream` should be encapsulating the graceful closure logic and handling that for you when you close the stream. All the `TcpClient` example code I've just looked up on the web seems to do it this way. – Peter Duniho Nov 08 '14 at 22:32

0 Answers0