Is there any way how can I set the timeout for the BinaryReader ?

When a disconnection unexpectedly occurs, and then it is restored, the client program sends the data, but the server part on BinaryReader.Read freezes and the program does not go further than this line. As soon as all clients disconnect, the program comes to life and starts loading everything, as if from a cache of some kind and ultimately gives an exception that the remote client has suddenly disconnected the connection.

  • And show the code, please. - VladD
  • one
    Perhaps you need this: stackoverflow.com/a/14378242/276994 - VladD
  • @VladD, the code is simple: TcpClien + NetworkStream is sent, TcpListener + NetworkStream + BinaryReader get the values. Minimum binding. I did as you advised in the link, added TcpClient.ReceiveTimeout to the value I needed and the program stopped hanging for a long time. Thank! - Nikolay
  • Ok, then I will issue as an answer. - VladD

1 answer 1

You cannot set the timeout for the BinaryReader itself, but you can set the timeout for the TcpClient , which delivers the carrier stream to it.

To do this, it makes sense to use the TcpClient.ReceiveTimeout parameter before the NetworkStream 's request.

The answer is honestly stolen from here .