NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) Method

Definition

Begins an asynchronous write to a stream.

public override IAsyncResult BeginWrite (byte[] buffer, int offset, int size, AsyncCallback? callback, object? state);
public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback? callback, object? state);
public override IAsyncResult BeginWrite (byte[] buffer, int offset, int size, AsyncCallback callback, object state);

Parameters

buffer
Byte[]

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

offset
Int32

The location in buffer to begin sending the data.

sizecount
Int32

The number of bytes to write to the NetworkStream.

callback
AsyncCallback

The AsyncCallback delegate that is executed when BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) completes.

state
Object

An object that contains any additional user-defined data.

Returns

An IAsyncResult that represents the asynchronous call.

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 underlying Socket is closed.

-or-

There was a failure while writing to the network.

-or-

An error occurred when accessing the socket.

Remarks

Vigtigt

This is a compatibility API, we don't recommend to use the APM (Begin / End) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginWrite in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginWrite. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginWrite method.

The BeginWrite operation must be completed by calling the EndWrite method. Typically, the method is invoked by the provided AsyncCallback delegate. EndWrite will block the calling thread until the operation is completed.

Bemærk

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.

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

Applies to

Produkt Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also