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

后端 未结 4 1336
执笔经年
执笔经年 2021-02-13 00:16

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

相关标签:
4条回答
  • 2021-02-13 01:06

    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.

    0 讨论(0)
  • 2021-02-13 01:07

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

    0 讨论(0)
  • 2021-02-13 01:09

    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?

    0 讨论(0)
  • 2021-02-13 01:12

    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.

    0 讨论(0)
提交回复
热议问题