是否需要同时关闭NetworkStream和TcpClient或仅关闭TcpClient? [英] Does one need to close both NetworkStream and TcpClient, or just TcpClient?

查看:183
本文介绍了是否需要同时关闭NetworkStream和TcpClient或仅关闭TcpClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关TcpClient.Close()的文档并注意到了这一点:

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


调用此方法最终将导致关闭关联的Socket,并且还将关闭关联的NetworkStream,如果已创建该网络,则用于发送和接收数据。

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.

请纠正我,如果我错了,但这表示如果在 TcpClient 上调用 Close(), c $ c> NetworkStream 也将被关闭。

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();

只调用 tcpClient.Close()会一样好吗?

推荐答案

回答此问题,因为没有人这样做,所以我可以接受答案。

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

根据Hans的说法,不需要调用NetworkStream.Close(),因为TcpClient.Close()会关闭其底层NetworkStream。

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

这篇关于是否需要同时关闭NetworkStream和TcpClient或仅关闭TcpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆