Stream.Read do not catch an Exception ?

  • Thread starter Thread starter Nicolas
  • Start date Start date
N

Nicolas

Hello,

I have to read data from a Stream class whose underlying type is a
NetworkStream class.
It works but when the client disconnects, no Exception is thrown. It
returns 0 but it should throw an Exception too.. I can't get it to work.

Thank you if you can provide some infos about this. BTW I use .net 2.0,
but it shouldn't be relevant in this case...

CODE ->

byte[] red_flow;
int red;

red = 0;
red_flow = new byte[4096];
try
{
if ((red = someStream.Read(red_flow, 0, red_flow.Length)) == 0)
{
Console.WriteLine("disconnected");
return;
}
}
catch
{
Console.WriteLine("disconnected");
return;
}
Console.WriteLine("not disconnected");
return;
 
Nicolas said:
I have to read data from a Stream class whose underlying type is a
NetworkStream class.
It works but when the client disconnects, no Exception is thrown. It
returns 0 but it should throw an Exception too.. I can't get it to work.

Why do you think it should throw an exception? If one end closes the
stream, returning 0 on read is exactly how it's normally indicated.
 
Back
Top