Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Net.​Sockets.​NetworkStream
Assembly: System.Net.Sockets
Inheritance: object → MarshalByRefObject → IO.​Stream → NetworkStream
Implemented Interfaces
- IAsyncDisposable Implemented by: Stream
- IDisposable Implemented by: Stream
Provides the underlying stream of data for network access.
Properties
public
bool
CanRead
Gets a value that indicates whether the <see cref="T:System.Net.Sockets.NetworkStream" /> supports reading.
public
bool
CanSeek
Gets a value that indicates whether the stream supports seeking. This property is not currently supported. This property always returns <see langword="false" /> .
public
bool
CanTimeout
Indicates whether timeout properties are usable for <see cref="T:System.Net.Sockets.NetworkStream" /> .
public
bool
CanWrite
Gets a value that indicates whether the <see cref="T:System.Net.Sockets.NetworkStream" /> supports writing.
public
bool
DataAvailable
Gets a value that indicates whether data is available on the <see cref="T:System.Net.Sockets.NetworkStream" /> to be immediately read.
public
long
Length
Gets the length of the data available on the stream. This property is not currently supported and always throws a <see cref="T:System.NotSupportedException" /> .
public
long
Position
Gets or sets the current position in the stream. This property is not currently supported and always throws a <see cref="T:System.NotSupportedException" /> .
protected
bool
Readable
Gets or sets a value that indicates whether the <see cref="T:System.Net.Sockets.NetworkStream" /> can be read.
public
int
ReadTimeout
Gets or sets the amount of time that a read operation blocks waiting for data.
public
Socket
Socket
Gets the underlying <see cref="T:System.Net.Sockets.Socket" /> .
protected
bool
Writeable
Gets a value that indicates whether the <see cref="T:System.Net.Sockets.NetworkStream" /> is writable.
public
int
WriteTimeout
Gets or sets the amount of time that a write operation blocks waiting for data.
Methods
public
IAsyncResult
BeginRead​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Begins an asynchronous read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
Returns An <see cref="T:System.IAsyncResult" /> that represents the asynchronous call.
buffer
An array of type <see cref="T:System.Byte" /> that is the location in memory to store data read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
offset
The location in <paramref name="buffer" /> to begin storing the data.
count
The number of bytes to read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
callback
The <see cref="T:System.AsyncCallback" /> delegate that is executed when <see cref="M:System.Net.Sockets.NetworkStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)" /> completes.
state
An object that contains any additional user-defined data.
public
IAsyncResult
BeginWrite​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Begins an asynchronous write to a stream.
Returns An <see cref="T:System.IAsyncResult" /> that represents the asynchronous call.
buffer
An array of type <see cref="T:System.Byte" /> that contains the data to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
offset
The location in <paramref name="buffer" /> to begin sending the data.
count
The number of bytes to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
callback
The <see cref="T:System.AsyncCallback" /> delegate that is executed when <see cref="M:System.Net.Sockets.NetworkStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)" /> completes.
state
An object that contains any additional user-defined data.
public
void
Close​(int timeout)
Closes the <see cref="T:System.Net.Sockets.NetworkStream" /> after waiting the specified time to allow data to be sent.
timeout
A 32-bit signed integer that specifies the number of milliseconds to wait to send any remaining data before closing.
public
void
Close​(TimeSpan timeout)
Closes the <see cref="T:System.Net.Sockets.NetworkStream" /> after waiting the specified amount of time to allow data to be sent.
timeout
The amount of time to wait to send any remaining data before closing.
protected
void
Dispose​(bool disposing)
Releases the unmanaged resources used by the <see cref="T:System.Net.Sockets.NetworkStream" /> and optionally releases the managed resources.
disposing
<see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.
public
int
EndRead​(IAsyncResult asyncResult)
Handles the end of an asynchronous read.
Returns The number of bytes read from the <see cref="T:System.Net.Sockets.NetworkStream" /> , between zero (0) and the number of bytes requested. The method returns zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown. If zero bytes are requested, read operations might complete immediately or might not complete until at least one byte is available (but without consuming any data).
asyncResult
An <see cref="T:System.IAsyncResult" /> that represents an asynchronous call.
public
void
EndWrite​(IAsyncResult asyncResult)
Handles the end of an asynchronous write.
asyncResult
The <see cref="T:System.IAsyncResult" /> that represents the asynchronous call.
protected
void
Finalize​()
Releases all resources used by the <see cref="T:System.Net.Sockets.NetworkStream" /> .
public
void
Flush​()
Flushes data from the stream. This method is reserved for future use.
FlushAsync​(Threading.​CancellationToken cancellationToken)
Flushes data from the stream as an asynchronous operation.
Returns The task object representing the asynchronous operation.
cancellationToken
A cancellation token used to propagate notification that this operation should be canceled.
public
int
Read​(byte[] buffer,
int offset,
int count)
Reads data from the <see cref="T:System.Net.Sockets.NetworkStream" /> and stores it to a byte array.
Returns The total number of bytes read into the buffer between zero (0) and the requested count. The method returns zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown.
buffer
An array of type <see cref="T:System.Byte" /> that is the location in memory to store data read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
offset
The location in <paramref name="buffer" /> to begin storing the data to.
count
The number of bytes to read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
public
int
Read​(Span<​byte> buffer)
Reads data from the <see cref="T:System.Net.Sockets.NetworkStream" /> and stores it to a span of bytes in memory.
Returns The total number of bytes read into the buffer, between zero (0) and the length of the buffer. The method returns zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown.
buffer
A region of memory to store data read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
Reads data from the <see cref="T:System.Net.Sockets.NetworkStream" /> and stores it to a specified range of a byte array as an asynchronous operation.
Returns A task that represents the asynchronous read operation. The value of its <see cref="P:System.Threading.Tasks.Task`1.Result" /> property contains the total number of bytes read into <paramref name="buffer" /> between zero (0) and the number of bytes requested. The result value is zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown. If zero bytes are requested, read operations may complete immediately or may not complete until at least one byte is available (but without consuming any data).
buffer
The buffer to write the data into.
offset
The location in <paramref name="buffer" /> to begin storing the data to.
count
The number of bytes to read from the <see cref="T:System.Net.Sockets.NetworkStream" /> .
cancellationToken
The token to monitor for cancellation requests.
public
Threading.​Tasks.​ValueTask<​int>
ReadAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Reads data from the <see cref="T:System.Net.Sockets.NetworkStream" /> and stores it in a byte memory range as an asynchronous operation.
Returns A <see cref="T:System.Threading.Tasks.ValueTask`1" /> that represents the asynchronous read operation. The value of its <see cref="P:System.Threading.Tasks.ValueTask`1.Result" /> property contains the total number of bytes read into <paramref name="buffer" /> between zero (0) and the number of bytes requested. The result value is zero (0) only if zero bytes were requested or if no more bytes are available because the peer socket performed a graceful shutdown. If zero bytes are requested, read operations may complete immediately or may not complete until at least one byte is available (but without consuming any data).
buffer
The buffer to write the data to.
cancellationToken
The token to monitor for cancellation requests.
public
int
ReadByte​()
Reads a byte from the <see cref="T:System.Net.Sockets.NetworkStream" /> and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
Returns The unsigned byte cast to an <see cref="T:System.Int32" /> , or -1 if at the end of the stream because the peer socket performed a graceful shutdown.
public
long
Seek​(long offset,
IO.​SeekOrigin origin)
Sets the current position of the stream to the given value. This method is not currently supported and always throws a <see cref="T:System.NotSupportedException" /> .
Returns The position in the stream.
offset
This parameter is not used.
origin
This parameter is not used.
public
void
SetLength​(long value)
Sets the length of the stream. This method always throws a <see cref="T:System.NotSupportedException" /> .
value
This parameter is not used.
public
void
Write​(byte[] buffer,
int offset,
int count)
Writes data to the <see cref="T:System.Net.Sockets.NetworkStream" /> from a specified range of a byte array.
buffer
An array of type <see cref="T:System.Byte" /> that contains the data to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
offset
The location in <paramref name="buffer" /> from which to start writing data.
count
The number of bytes to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
public
void
Write​(ReadOnlySpan<​byte> buffer)
Writes data to the <see cref="T:System.Net.Sockets.NetworkStream" /> from a read-only byte span.
buffer
The data to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
WriteAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
Writes data to the <see cref="T:System.Net.Sockets.NetworkStream" /> from the specified range of a byte array as an asynchronous operation.
Returns A task that represents the asynchronous write operation.
buffer
A byte array that contains the data to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
offset
The location in <paramref name="buffer" /> from which to start writing data.
count
The number of bytes to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
cancellationToken
The token to monitor for cancellation requests.
WriteAsync​(ReadOnlyMemory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Writes data to the <see cref="T:System.Net.Sockets.NetworkStream" /> from a read-only memory byte memory range as an asynchronous operation.
Returns A task that represents the asynchronous write operation.
buffer
A region of memory that contains the data to write to the <see cref="T:System.Net.Sockets.NetworkStream" /> .
cancellationToken
The token to monitor for cancellation requests.
public
void
WriteByte​(byte value)
Writes a byte to the current position in the <see cref="T:System.Net.Sockets.NetworkStream" /> and advances the position within the stream by one byte.
value
The byte to write to the stream.
public
void
CopyTo​(IO.​Stream destination)
Inherited from IO.​Stream
public
void
CopyTo​(IO.​Stream destination,
int bufferSize)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
int bufferSize)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
int bufferSize,
Threading.​CancellationToken cancellationToken)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
Threading.​CancellationToken cancellationToken)
Inherited from IO.​Stream
protected
Threading.​WaitHandle
CreateWaitHandle​()
Inherited from IO.​Stream
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
public
int
ReadAtLeast​(Span<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true)
Inherited from IO.​Stream
public
Threading.​Tasks.​ValueTask<​int>
ReadAtLeastAsync​(Memory<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
public
void
ReadExactly​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
public
void
ReadExactly​(Span<​byte> buffer)
Inherited from IO.​Stream
ReadExactlyAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
ReadExactlyAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
WriteAsync​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
protected
MarshalByRefObject
MemberwiseClone​(bool cloneIdentity)
Inherited from MarshalByRefObject
public
bool
Equals​(object obj)
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object