1

I use NetworkStream with sockets in an SslStream socket server as follows:

stream = new NetworkStream(socket, true); 
sslStream = new SslStream(stream, false);

My question is, if when I call sslStream.Dispose(), will the SslStream dispose/close its inner stream and its socket too?

Or do I need to explicitly close all three resources with sslStream.Close(), stream.Close() and socket.Close()?

g t
  • 7,287
  • 7
  • 50
  • 85
dabididabidi
  • 41
  • 1
  • 6

1 Answers1

4

If possible, you should use C#'s using construct to dispose the stream 'automatically' after use:

using (var sslStream = new SslStream(stream, false))
{
    // read from stream here...
}

However, if you want to keep the SslStream for later use, you will have to dispose manually.

Disposing a stream typically closes the stream as well, the Close() method seems to be there mostly for completeness. You can download the .NET source code (or use a decompiler), and examine the SslStream.cs and its base class AuthenticatedStream.cs to see the exact behaviour.

To answer the rest of your question - The SslStream is well documented on Microsoft's MSDN site which shows one of SslStream's constructors takes two parameters (also shown by your example). The first parameter is the inner stream - which in your example the NetworkStream object. The second is a boolean called leaveInnerStreamOpen. In your example, you pass false.

It is this second value that determines the behaviour you are asking about: If you pass true, the inner stream will remain open when you close/dipose the SslStream (the stream will also be flushed). If you pass false, then the inner stream will be closed too.

Similarly for the NetworkStream, its constructor also takes a boolean ownsSocket. If this is set to true (as in your example), then disposing/closing the NetworkStream will also close its socket, otherwise it stays open.

Therefore, as your example code stands, you must call both sslStream.Dispose() and stream.Dispose() (the socket is closed automatically).

However, if you change the second parameter in the SslStream's constructor to true in your example code, you can just call sslStream.Dispose() to close the SslStream, the NetworkStream, and its socket.

g t
  • 7,287
  • 7
  • 50
  • 85
  • ssl constructor true means leave inner stream open, that mean eventhough sslstream is closed , inner stream remains open.I think sslstream construnctor should have false inorder to close inner stream. Thanks – dabididabidi Jul 26 '12 at 19:37
  • What is the default value of leaveInnerStreamOpen? – daisy Jan 12 '20 at 07:51
  • @daisy There is no 'default' really, as the caller must decide what to pass to the constructor (depending on how the underlying stream is being used). If you just want to use `SslStream` and don't care about the 'leave inner stream open' behavior, then use the other constructor without this parameter, like: `new SslStream(stream)`. The framework will then call the other constructor with a 'default' value of `leaveInnerStreamOpen`=`false`. This is like handing over the inner stream you created to the SslStream object and saying "you are in charge of this stream now, not me". – g t Jan 13 '20 at 08:04