NetworkStream.Close 方法

定义

重载

Close()

关闭 NetworkStream

Close(Int32)

等待指定的时间获许发送数据之后,关闭 NetworkStream

Close(TimeSpan)

NetworkStream在等待指定时间后关闭 以允许发送数据。

Close()

关闭 NetworkStream

C#
public override void Close();

示例

下面的代码示例关闭 NetworkStream

C#
// Example for closing the NetworkStream.

// Close the NetworkStream
myNetworkStream.Close();

注解

方法 Close 释放与 NetworkStream关联的非托管资源和托管资源。 NetworkStream如果 拥有基础 Socket,则它也会关闭。

NetworkStream如果 与 TcpClient关联, 方法Close将关闭 TCP 连接,但不释放关联的 TcpClient

适用于

.NET Framework 1.1
产品 版本
.NET Framework 1.1

Close(Int32)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

等待指定的时间获许发送数据之后,关闭 NetworkStream

C#
public void Close(int timeout);

参数

timeout
Int32

一个 32 位带符号整数,用于指定在关闭前等待发送所有剩余数据的毫秒数。

例外

timeout 参数小于 -1。

注解

方法 Close 释放与 NetworkStream关联的非托管资源和托管资源。 NetworkStream如果 拥有基础 Socket,则它也会关闭。

NetworkStream如果 与 TcpClient关联, 方法Close将关闭 TCP 连接,但不释放关联的 TcpClient

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Close(TimeSpan)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

NetworkStream在等待指定时间后关闭 以允许发送数据。

C#
public void Close(TimeSpan timeout);

参数

timeout
TimeSpan

在关闭之前等待发送任何剩余数据的时间量。

例外

timeout 小于 -1 毫秒或大于 MaxValue 毫秒。

注解

Close 方法释放与 NetworkStream关联的非托管资源和托管资源。 NetworkStream如果 拥有基础 Socket,则它也会关闭。 NetworkStream如果 与 TcpClient关联, 方法Close(Int32)将关闭 TCP 连接,但不释放关联的 TcpClient

适用于

.NET 10 和其他版本
产品 版本
.NET 7, 8, 9, 10