Table of Contents

Class NetworkStream

Namespace
System.Net.Sockets
Assembly
System.Net.dll

Provides the underlying stream of data for network access.

public class NetworkStream : Stream, IDisposable
Inheritance
NetworkStream
Implements
Derived
Inherited Members
Extension Methods

Constructors

NetworkStream(Socket)

Creates a new instance of the NetworkStream class for the specified Socket.

public NetworkStream(Socket socket)

Parameters

socket Socket

The Socket that the NetworkStream will use to send and receive data.

NetworkStream(Socket, bool)

Initializes a new instance of the NetworkStream class for the specified Socket with the specified Socket ownership.

public NetworkStream(Socket socket, bool ownsSocket)

Parameters

socket Socket

The Socket that the NetworkStream will use to send and receive data.

ownsSocket bool

true to indicate that the NetworkStream will take ownership of the Socket; otherwise, false.

Fields

_disposed

Internal disposed flag

protected bool _disposed

Field Value

bool

_remoteEndPoint

Internal endpoint ref used for dgram sockets

protected EndPoint _remoteEndPoint

Field Value

EndPoint

_socketType

Internal property used to store the socket type

protected int _socketType

Field Value

int

Properties

CanRead

Gets a value that indicates whether the System.Net.Sockets.NetworkStream supports reading.

public override bool CanRead { get; }

Property Value

bool

true if data can be read from the stream; otherwise, false. The default value is true.

Remarks

If CanRead is true, NetworkStream allows calls to the Read(byte[], int, int) method. Provide the appropriate FileAccess enumerated value in the constructor to set the readability and write-ability of the NetworkStream. The CanRead property is set when the NetworkStream is initialized.

CanSeek

Gets a value that indicates whether the stream supports seeking. This property is not currently supported.This property always returns false.

public override bool CanSeek { get; }

Property Value

bool

false in all cases to indicate that System.Net.Sockets.NetworkStream cannot seek a specific location in the stream.

CanTimeout

Indicates whether timeout properties are usable for System.Net.Sockets.NetworkStream.

public override bool CanTimeout { get; }

Property Value

bool

true in all cases.

CanWrite

Gets a value that indicates whether the System.Net.Sockets.NetworkStream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if data can be written to the System.Net.Sockets.NetworkStream; otherwise, false. The default value is true.

DataAvailable

Gets a value that indicates whether data is available on the NetworkStream to be read.

public virtual bool DataAvailable { get; }

Property Value

bool

true if data is available on the stream to be read; otherwise, false.

Length

Gets the length of the data available on the stream. This property is not currently supported and always throws a NotSupportedException.

public override long Length { get; }

Property Value

long

The length of the data available on the stream.

Position

Gets or sets the current position in the stream. This property is not currently supported and always throws a NotSupportedException.

public override long Position { get; set; }

Property Value

long

The current position in the stream.

ReadTimeout

Gets or sets the amount of time that a read operation blocks waiting for data.

public override int ReadTimeout { get; set; }

Property Value

int

A Int32 that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, Infinite, specifies that the read operation does not time out.

WriteTimeout

Gets or sets the amount of time that a write operation blocks waiting for data.

public override int WriteTimeout { get; set; }

Property Value

int

A Int32 that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. The default value, Infinite, specifies that the write operation does not time out.

Methods

Close(int)

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

public void Close(int timeout)

Parameters

timeout int

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

Remarks

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

Dispose(bool)

Releases the unmanaged resources used by the NetworkStream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks

This method is called by the public Dispose method and the Finalize method. Dispose invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Finalize invokes Dispose with disposing set to false.

When the disposing parameter is true, this method releases all resources held by any managed objects that this NetworkStream references. This method invokes the Dispose method of each referenced object.

Flush()

Flushes data from the stream. This method is reserved for future use.

public override void Flush()

Read(byte[], int, int)

Reads data from the NetworkStream.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of type Byte that is the location in memory to store data read from the NetworkStream.

offset int

The location in buffer to begin storing the data to.

count int

The number of bytes to read from the NetworkStream.

Returns

int

The number of bytes read from the NetworkStream.

Remarks

This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. The Read operation reads as much data as is available, up to the number of bytes specified by the count parameter. If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.

important

Check to see if the NetworkStream is readable by calling the CanRead property. If you attempt to read from a NetworkStream that is not readable, you will get an IOException.

Read(SpanByte)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(SpanByte buffer)

Parameters

buffer SpanByte

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Seek(long, SeekOrigin)

Sets the current position of the stream to the given value. This method is not currently supported and always throws a System.NotSupportedException.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

This parameter is not used.

origin SeekOrigin

This parameter is not used.

Returns

long

The position in the stream.

SetLength(long)

Sets the length of the stream. This method always throws a System.NotSupportedException.

public override void SetLength(long value)

Parameters

value long

This parameter is not used.

Write(byte[], int, int)

Writes data to the NetworkStream.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of type Byte that contains the data to write to the NetworkStream.

offset int

The location in buffer from which to start writing data.

count int

The number of bytes to write to the NetworkStream.

Remarks

The Write method starts at the specified offset and sends count bytes from the contents of buffer to the network. The Write method blocks until the requested number of bytes is sent or a SocketException is thrown. If you receive a SocketException, use the ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.