You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@reef.apache.org by Dhruv Mahajan <dh...@gmail.com> on 2016/06/09 01:24:22 UTC

C# NetworkStream.close() behavior

Hi

I have a question regrading a network stream behavior.
Suppose on the client side in the end I do:

int a=5;
Networkstream.write(GetBytes(a));
Networkstream.close();

Now on the server side:

I have

byte[] buffer = new byte[1024];
while(int read = networkstream.ReadAsync(buffer,0,1024,token))
{
   if (read == 0)
   {
      return; // stream is closed
   }
   else
   {
      DoSomething();
   }
}

It seems that even of read==0, there are bytes left to read. My
understanding on reading the documentation was that no further data is
available since stream is closed. Any idea where am I going wrong?

Dhruv

Re: C# NetworkStream.close() behavior

Posted by Markus Weimer <ma...@weimo.de>.
On 2016-06-08 18:24, Dhruv Mahajan wrote:
> It seems that even of read==0, there are bytes left to read. My
> understanding on reading the documentation was that no further data
> is available since stream is closed. Any idea where am I going
> wrong?

The documentation[0] states:

> Instead of calling this method, ensure that the stream is properly
> disposed.

So, you might want to use `.Dispose()` instead?

Markus

[0]: 
https://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.close%28v=vs.110%29.aspx