NetworkStream.Close Method

Definition

Overloads

Close()

Closes the NetworkStream.

Close(Int32)

Closes the NetworkStream after waiting the specified time to allow data to be sent.

Close(TimeSpan)

Closes the NetworkStream after waiting the specified amount of time to allow data to be sent.

Close()

Closes the NetworkStream.

C#
public override void Close();

Examples

The following code example closes the NetworkStream.

C#
// Example for closing the NetworkStream.

// Close the NetworkStream
myNetworkStream.Close();

Remarks

The Close method frees both unmanaged and managed resources associated with the NetworkStream. If the NetworkStream owns the underlying Socket, it is closed as well.

If a NetworkStream was associated with a TcpClient, the Close method will close the TCP connection, but not dispose of the associated TcpClient.

Applies to

.NET Framework 1.1
Product Versions
.NET Framework 1.1

Close(Int32)

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

Closes the NetworkStream after waiting the specified time to allow data to be sent.

C#
public void Close(int timeout);

Parameters

timeout
Int32

A 32-bit signed integer that specifies the number of milliseconds to wait to send any remaining data before closing.

Exceptions

The timeout parameter is less than -1.

Remarks

The Close method frees both unmanaged and managed resources associated with the NetworkStream. If the NetworkStream owns the underlying Socket, it is closed as well.

If a NetworkStream was associated with a TcpClient, the Close method will close the TCP connection, but not dispose of the associated TcpClient.

Applies to

.NET 10 and other versions
Product Versions
.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

Closes the NetworkStream after waiting the specified amount of time to allow data to be sent.

C#
public void Close(TimeSpan timeout);

Parameters

timeout
TimeSpan

The amount of time to wait to send any remaining data before closing.

Exceptions

timeout is less than -1 milliseconds or greater than MaxValue milliseconds.

Remarks

The Close method frees both unmanaged and managed resources associated with the NetworkStream. If the NetworkStream owns the underlying Socket, it is closed as well. If a NetworkStream was associated with a TcpClient, the Close(Int32) method will close the TCP connection, but not dispose of the associated TcpClient.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10