Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

NetworkStream.WriteAsync Method

Definition

Namespace:
System.Net.Sockets
Assemblies:
netstandard.dll, System.Net.Sockets.dll
Assembly:
System.Net.Sockets.dll
Assembly:
netstandard.dll

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

NameDescription
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

Writes data to theNetworkStream from a read-only memory byte memory range as an asynchronous operation.

WriteAsync(Byte[], Int32, Int32, CancellationToken)

Writes data to theNetworkStream 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 theNetworkStream 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 theNetworkStream.

cancellationToken
CancellationToken

The token to monitor for cancellation requests.

Returns

A task that represents the asynchronous write operation.

Exceptions

TheNetworkStream does not support writing.

An error occurred when accessing the socket.

-or-

There was a failure while writing to the network.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method sends all bytes inbuffer to the network.

Note

Check to see if theNetworkStream is writable by calling theCanWrite property. If you attempt to write to aNetworkStream that is not writable, you will get anInvalidOperationException.

Note

If you receive anIOException, check theInnerException property to determine if it was caused by aSocketException. If so, use theErrorCode 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 theNetworkStream 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 theNetworkStream.

offset
Int32

The location inbuffer from which to start writing data.

countsize
Int32

The number of bytes to write to theNetworkStream.

cancellationToken
CancellationToken

The token to monitor for cancellation requests.

Returns

A task that represents the asynchronous write operation.

Exceptions

Thebuffer parameter isnull.

Theoffset parameter is less than 0.

-or-

Theoffset parameter is greater than the length ofbuffer.

-or-

Thesize parameter is less than 0.

-or-

Thesize parameter is greater than the length ofbuffer minus the value of theoffset parameter.

TheNetworkStream does not support writing.

There was a failure while writing to the network.

-or-

An error occurred when accessing the socket.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method starts at the specifiedoffset and sendscount bytes from the contents ofbuffer to the network.

Note

Check to see if theNetworkStream is writable by calling theCanWrite property. If you attempt to write to aNetworkStream that is not writable, you will get anInvalidOperationException.

Note

If you receive anIOException, check theInnerException property to determine if it was caused by aSocketException. If so, use theErrorCode 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 asArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown byWrite(Byte[], Int32, Int32).

Applies to

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?