I have a full duplex connection between two hosts that exchange datas with a thread that always listen on NetworkStream.Read(). How can I close gracefully the connection avoiding:
1- the deadlock of the two side read() function
2- the abort() of reading threads [closing they safely]
I think closing the connection with SocketShutdown.Send but how it works? It unlock the Read() on the other side.. There are not many things in the documentation.
Thak you
A simple network exchange looks something like this:
Now the connection is established. There will be some defined protocol for communicating with each other. That protocol will dictate who starts. It can be either, but for this example, let's assuming it's the client:
Answering your specific questions:
How is deadlock avoided?
There's no deadlock potential here. Servers and clients both are often written using asynchronous networking APIs. But if the defined protocol is a straightforward request/response protocol (e.g. HTTP), even that's not strictly required. Each end can be single-threaded, alternating between sending and receiving as appropriate for the protocol.'How do you abort the reading threads?` You don't. If you are indeed dedicating a whole thread to reading from the socket (and that's really not very good design), then the thread simply exits when it sees a receive operation complete with 0 bytes as the length.