C# - 要关闭 NetworkStream,请调用stream.Close 或socket.Shutdown?

发布于 2024-10-18 06:31:42 字数 1055 浏览 2 评论 0原文

我想知道是否有人知道处理使用 Socket 对象和 NetworkStream 对象的类的最佳方法?相关类有一个 NetworkStream 实例和一个用于创建 NetworkStream 的 Socket 实例。

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

在我的 Dispose 方法中,我应该这样做吗?

this.stream.Close();
this.socket.Shutdown(SocketShutdown.Both);
this.socket.Close();

这一切是必要的还是多余的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-10-25 06:31:42

Socket 不会处置关联的 NetworkStream。我也确定启动了reflector。 (一个分析 .NET dll 和 .NET 库的工具。很棒的工具。你必须在月底之前下载免费版本,然后才能完全商业化)。

但是,根据 MDSN 文档 和反射器,流将关闭套接字,但前提是它拥有套接字的所有权。您可以将其设置为重载构造函数中的第二个参数。

无论如何,您都必须调用 Shutdown,因为如果 刷新数据。如果不这样做,您可能会丢失数据。

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.

何止钟意 2024-10-25 06:31:42

SocketStream 都实现了 IDisposable,因此您只需在每个对象上调用 .Dispose() 即可。 Dispose 方法应该处理关闭和其他处置所需的操作。

this.stream.Dispose(); 
this.socket.Dispose(); 

例如,这是流类中反汇编的 Dispose 方法:

public void Dispose()
{
    this.Close();
}

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();
}
眼睛会笑 2024-10-25 06:31:42

根据MSDN中的文档,调用stream.Close()“关闭当前流并释放与当前流关联的任何资源(例如套接字和文件句柄)。”,这告诉我stream.Close()还释放了插座。不过,您仍然需要调用 socket.Shutdown() 。

无论如何,恕我直言,最安全的方法是使用“使用”,它可以让您保持在安全的网站上:)

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文