Does one need to close both NetworkStream and TcpClient, or just TcpClient?

江枫思渺然 提交于 2019-12-03 11:27:57

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.

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.

Vivien Ruiz

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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!