NetworkStream.WriteAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
Writes data to the NetworkStream from a read-only memory byte memory range as an asynchronous operation. |
| WriteAsync(Byte[], Int32, Int32, CancellationToken) |
Writes data to the NetworkStream from the specified range of a byte array as an asynchronous operation. |
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
Writes data to the NetworkStream from a read-only memory byte memory range as an asynchronous operation.
public override System.Threading.Tasks.ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask
Public Overrides Function WriteAsync (buffer As ReadOnlyMemory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask
Parameters
- buffer
- ReadOnlyMemory<Byte>
A region of memory that contains the data to write to the NetworkStream.
- cancellationToken
- CancellationToken
The token to monitor for cancellation requests.
Returns
A task that represents the asynchronous write operation.
Exceptions
The NetworkStream does not support writing.
An error occurred when accessing the socket.
-or-
There was a failure while writing to the network.
The NetworkStream is closed.
The cancellation token was canceled. This exception is stored into the returned task.
Remarks
This method sends all bytes in buffer to the network.
Note
Check to see if the NetworkStream is writable by calling the CanWrite property. If you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.
Note
If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.
Applies to
WriteAsync(Byte[], Int32, Int32, CancellationToken)
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
Writes data to the NetworkStream from the specified range of a byte array as an asynchronous operation.
public:
override System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
public:
override System::Threading::Tasks::Task ^ WriteAsync(cli::array <System::Byte> ^ buffer, int offset, int size, System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task WriteAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
public override System.Threading.Tasks.Task WriteAsync(byte[] buffer, int offset, int size, System.Threading.CancellationToken cancellationToken);
override this.WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
override this.WriteAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function WriteAsync (buffer As Byte(), offset As Integer, count As Integer, cancellationToken As CancellationToken) As Task
Public Overrides Function WriteAsync (buffer As Byte(), offset As Integer, size As Integer, cancellationToken As CancellationToken) As Task
Parameters
- buffer
- Byte[]
A byte array that contains the data to write to the NetworkStream.
- offset
- Int32
The location in buffer from which to start writing data.
- countsize
- Int32
The number of bytes to write to the NetworkStream.
- cancellationToken
- CancellationToken
The token to monitor for cancellation requests.
Returns
A task that represents the asynchronous write operation.
Exceptions
The buffer parameter is null.
The offset parameter is less than 0.
-or-
The offset parameter is greater than the length of buffer.
-or-
The size parameter is less than 0.
-or-
The size parameter is greater than the length of buffer minus the value of the offset parameter.
The NetworkStream does not support writing.
There was a failure while writing to the network.
-or-
An error occurred when accessing the socket.
The NetworkStream is closed.
The cancellation token was canceled. This exception is stored into the returned task.
Remarks
This method starts at the specified offset and sends count bytes from the contents of buffer to the network.
Note
Check to see if the NetworkStream is writable by calling the CanWrite property. If you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.
Note
If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.
This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Write(Byte[], Int32, Int32).