Class NetworkStream
Provides the underlying stream of data for network access.
Implements
Inherited Members
Namespace: System.Net.Sockets
Assembly: System.Net.dll
Syntax
public class NetworkStream : Stream, IDisposable
Constructors
| Improve this Doc View SourceNetworkStream(Socket)
Creates a new instance of the NetworkStream class for the specified Socket.
Declaration
public NetworkStream(Socket socket)
Parameters
Type | Name | Description |
---|---|---|
Socket | socket | The Socket that the NetworkStream will use to send and receive data. |
NetworkStream(Socket, Boolean)
Initializes a new instance of the NetworkStream class for the specified Socket with the specified Socket ownership.
Declaration
public NetworkStream(Socket socket, bool ownsSocket)
Parameters
Type | Name | Description |
---|---|---|
Socket | socket | The Socket that the NetworkStream will use to send and receive data. |
Boolean | ownsSocket | true to indicate that the NetworkStream will take ownership of the Socket; otherwise, false. |
Fields
| Improve this Doc View Source_disposed
Internal disposed flag
Declaration
protected bool _disposed
Field Value
Type | Description |
---|---|
Boolean |
_remoteEndPoint
Internal endpoint ref used for dgram sockets
Declaration
protected EndPoint _remoteEndPoint
Field Value
Type | Description |
---|---|
EndPoint |
_socketType
Internal property used to store the socket type
Declaration
protected int _socketType
Field Value
Type | Description |
---|---|
Int32 |
Properties
| Improve this Doc View SourceCanRead
Gets a value that indicates whether the System.Net.Sockets.NetworkStream supports reading.
Declaration
public override bool CanRead { get; }
Property Value
Type | Description |
---|---|
Boolean | true if data can be read from the stream; otherwise, false. The default value is true. |
Overrides
Remarks
If CanRead is true, NetworkStream allows calls to the Read(Byte[], Int32, Int32) method. Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability 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.
Declaration
public override bool CanSeek { get; }
Property Value
Type | Description |
---|---|
Boolean | false in all cases to indicate that System.Net.Sockets.NetworkStream cannot seek a specific location in the stream. |
Overrides
| Improve this Doc View SourceCanTimeout
Indicates whether timeout properties are usable for System.Net.Sockets.NetworkStream.
Declaration
public override bool CanTimeout { get; }
Property Value
Type | Description |
---|---|
Boolean | true in all cases. |
Overrides
| Improve this Doc View SourceCanWrite
Gets a value that indicates whether the System.Net.Sockets.NetworkStream supports writing.
Declaration
public override bool CanWrite { get; }
Property Value
Type | Description |
---|---|
Boolean | true if data can be written to the System.Net.Sockets.NetworkStream; otherwise, false. The default value is true. |
Overrides
| Improve this Doc View SourceDataAvailable
Gets a value that indicates whether data is available on the NetworkStream to be read.
Declaration
public virtual bool DataAvailable { get; }
Property Value
Type | Description |
---|---|
Boolean | 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.
Declaration
public override long Length { get; }
Property Value
Type | Description |
---|---|
Int64 | The length of the data available on the stream. |
Overrides
| Improve this Doc View SourcePosition
Gets or sets the current position in the stream. This property is not currently supported and always throws a NotSupportedException.
Declaration
public override long Position { get; set; }
Property Value
Type | Description |
---|---|
Int64 | The current position in the stream. |
Overrides
| Improve this Doc View SourceReadTimeout
Gets or sets the amount of time that a read operation blocks waiting for data.
Declaration
public override int ReadTimeout { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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. |
Overrides
| Improve this Doc View SourceWriteTimeout
Gets or sets the amount of time that a write operation blocks waiting for data.
Declaration
public override int WriteTimeout { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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. |
Overrides
Methods
| Improve this Doc View SourceClose(Int32)
Closes the NetworkStream after waiting the specified time to allow data to be sent.
Declaration
public void Close(int timeout)
Parameters
Type | Name | Description |
---|---|---|
Int32 | timeout | A 32-bit signed integer that specifies the number of milliseconds to wait to send any remaining data before closing. |
Remarks
The Close(Int32) method frees both unmanaged and managed resources associated with the NetworkStream. If the NetworkStream owns the underlying Socket, it is closed as well.
Dispose(Boolean)
Releases the unmanaged resources used by the NetworkStream and optionally releases the managed resources.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
Boolean | disposing | true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Overrides
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.
Declaration
public override void Flush()
Overrides
| Improve this Doc View SourceRead(Byte[], Int32, Int32)
Reads data from the NetworkStream.
Declaration
public override int Read(byte[] buffer, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | An array of type Byte that is the location in memory to store data read from the NetworkStream. |
Int32 | offset | The location in buffer to begin storing the data to. |
Int32 | count | The number of bytes to read from the NetworkStream. |
Returns
Type | Description |
---|---|
Int32 | The number of bytes read from the NetworkStream. |
Overrides
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.
Declaration
public override int Read(SpanByte buffer)
Parameters
Type | Name | Description |
---|---|---|
SpanByte | buffer | A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source. |
Returns
Type | Description |
---|---|
Int32 | 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. |
Overrides
| Improve this Doc View SourceSeek(Int64, SeekOrigin)
Sets the current position of the stream to the given value. This method is not currently supported and always throws a System.NotSupportedException.
Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type | Name | Description |
---|---|---|
Int64 | offset | This parameter is not used. |
SeekOrigin | origin | This parameter is not used. |
Returns
Type | Description |
---|---|
Int64 | The position in the stream. |
Overrides
| Improve this Doc View SourceSetLength(Int64)
Sets the length of the stream. This method always throws a System.NotSupportedException.
Declaration
public override void SetLength(long value)
Parameters
Type | Name | Description |
---|---|---|
Int64 | value | This parameter is not used. |
Overrides
| Improve this Doc View SourceWrite(Byte[], Int32, Int32)
Writes data to the NetworkStream.
Declaration
public override void Write(byte[] buffer, int offset, int count)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | buffer | An array of type Byte that contains the data to write to the NetworkStream. |
Int32 | offset | The location in buffer from which to start writing data. |
Int32 | count | The number of bytes to write to the NetworkStream. |
Overrides
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.