TcpClient.GetStream()

Its best practice, you remember, to dispose of anything that implements IDisposable when you’re done with it.  The existence of the interface is a signal to the programmer that the garbage collector won’t clean this up after you.  If you don’t dispose of it, you’ll introduce a memory leak.

It’s only natural, then, when using TcpClient.GetStream, to want to dispose of the stream when you’re done with it.  However, this will close the connection, so, if you break your logic into a Read function and a Write function, you won’t have a good time.

This code doesn’t work:

Because the using will destroy the underlying network stream, closing the connection.  When you get to the Read(), an exception will be thrown because the connection is already closed.

Remember – you still want to close the socket when you’re finished!  The garbage collector won’t close the socket (because its unmanaged), so you do have to be conscious of this.  The easiest way is to just call Close() on the TcpClient.

This code does work:

Leave a Reply

Your email address will not be published. Required fields are marked *