Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 624
Nick Ramirez Avatar asked Feb 18 '11 22:02

Nick Ramirez


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

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.

like image 150
Sem Vanmeenen Avatar answered Oct 12 '22 23:10

Sem Vanmeenen


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();
}
like image 37
Kyle Trauberman Avatar answered Oct 13 '22 00:10

Kyle Trauberman


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);
}
like image 35
KBoek Avatar answered Oct 13 '22 01:10

KBoek