18

I'm reading the documentation on TcpClient.Close() and noticed this:

Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created.

So, correct me if I'm wrong but this says that if one calls Close() on the TcpClient that the NetworkStream will also be closed.

So then why at the end of the code example are both Close() called?

networkStream.Close();
tcpClient.Close();

Would it be just as fine to only call tcpClient.Close();?

user247702
  • 23,641
  • 15
  • 110
  • 157
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • 2
    Yes, it isn't necessary. Using tcpClient.Close() in that example isn't necessary either, the program terminates right after that :) – Hans Passant Oct 01 '11 at 00:01
  • 5
    Prior to .NET 3.0, both the TcpClient and the underlying NetworkStream had to be explicitly closed. Beginning in .NET 3.0, the underlying NetworkStream is closed when closing the TcpClient. Close() and Dispose() on TcpClient are equivalent, with Close() adding some internal logging. – Matt Davis Feb 02 '13 at 19:06

4 Answers4

18

Responding to this question since no one else did so that I may accept an answer.

According to Hans, calling NetworkStream.Close() is unnecessary because TcpClient.Close() closes its underlying NetworkStream.

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • 2
    This answer is incorrect. See Vivien's answer. As noted there, the [doc for GetStream](http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream.aspx) states `You must close the NetworkStream when you are through sending and receiving data. Closing TcpClient does not release the NetworkStream.` – BitMask777 Oct 31 '12 at 18:28
  • 5
    However, the [doc for Close](http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.close.aspx) states `Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created.` I suspect the GetStream doc is out of date - that note has been there since .NET 2.0 (when it was still an issue). – Sam Jul 24 '13 at 13:01
  • 1
    I had the same question and found this post, the reference source code confirms that TcpClient.Close also calls Close of the NetworkStream, so this answer is correct. https://referencesource.microsoft.com/#System/net/System/Net/Sockets/TCPClient.cs,943f9db3a7511918 – R1PFake Sep 22 '18 at 21:09
2

Closing the client does not close the stream, it's in the doc of the GetStream method. For the reference, have a look to this discussion : How to properly and completely close/reset a TcpClient connection?

Community
  • 1
  • 1
Vivien Ruiz
  • 618
  • 3
  • 11
  • [As Sam writes here](http://stackoverflow.com/questions/7617127/does-one-need-to-close-both-networkstream-and-tcpclient-or-just-tcpclient#comment26031079_7623611), that note is out-of-date. It used to be necessary, but since .NET 3.0 it no longer is. – user247702 May 14 '17 at 23:18
2

NetworkStream and TcpClient implement IDisposable. So best practise in my opinion is to pack it into a using block, so you never need to close or dispose it manually.

NickD
  • 2,672
  • 7
  • 37
  • 58
-5

You should Just do "TcpClient.Close()"
Or if you must, type both

user975017
  • 89
  • 1
  • 1
  • 3