Nick Ramirez
Nick Ramirez

Reputation: 483

C# - To close a NetworkStream, call stream.Close or socket.Shutdown?

I was wondering if anyone knew the best way to dispose of a class that uses a Socket object and NetworkStream object? The class in question has an instance of NetworkStream and an instance of Socket that's used to create the NetworkStream.

this.socket = new Socket(
                           AddressFamily.InterNetwork,
                           SocketType.Stream,
                           ProtocolType.Tcp)
                        {
                            ReceiveBufferSize = 65536,
                            SendBufferSize = 150
                        };

this.socket.Connect(
                        new IPEndPoint(                                                                   
                              IPAddress.Parse(
                                          Properties.Settings.Default.MpsServer2),
                                          Properties.Settings.Default.MpsPort2));

this.stream = new NetworkStream(
                         this.socket, true);

In my Dispose method, should I do this?

this.stream.Close();
this.socket.Shutdown(SocketShutdown.Both);
this.socket.Close();

Is all of this necessary or is it overkill?

Upvotes: 9

Views: 9073

Answers (3)

KBoek
KBoek

Reputation: 5985

According to the documentation in MSDN, calling stream.Close() "Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.", which tells me that stream.Close() also disposes the socket. You'd still have to call socket.Shutdown() though.

Anyway, IMHO the safest way is using "using", it keeps you on the safe site :)

using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                        {
                            ReceiveBufferSize = 65536,
                            SendBufferSize = 150
                        };) {
  socket.Connect(
    new IPEndPoint(
      IPAddress.Parse(
        Properties.Settings.Default.MpsServer2), Properties.Settings.Default.MpsPort2));

  using (var stream = new NetworkStream(socket, true) {
    // do something with the stream here
  }

  socket.Shutdown(SocketShutdown.Both);
}

Upvotes: 1

Sem Vanmeenen
Sem Vanmeenen

Reputation: 2151

Socket does not dispose of the associated NetworkStream. I fired up reflector too be certain. (a tool to analyze .NET dlls and thet .NET libraries. Great tool. You have to end of the month to download the free version before it goes fully. commercial).

However, according to both the MDSN documentation and reflector the stream will close the socket but only if it has ownership of the socket. You can set that as the second parameter in the overloaded constructor.

You have to call Shutdown in any case because if flushes the data. If you don't, you could lose data.

Upvotes: 2

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

Both Socket and Stream Implement IDisposable so you can just call .Dispose() on each object. The Dispose method should handle closing and other actions necessary for disposal.

this.stream.Dispose(); 
this.socket.Dispose(); 

For Example, this is the disassembled Dispose method from the stream class:

public void Dispose()
{
    this.Close();
}

Upvotes: 1

Related Questions