System.Net.Sockets.NetworkStream.EndWrite Method

Handles the end of an asynchronous write.

Syntax

public override void EndWrite (IAsyncResult ar)

Parameters

ar
A IAsyncResult object that holds the state information for the asynchronous operation.

Exceptions

TypeReason
ArgumentNullException asyncResult is null.
System.IO.IOException
Note: This method catches all exceptions thrown by the Socket.EndSend(IAsyncResult) method.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

NetworkStream.EndWrite(IAsyncResult) completes the asynchronous send operation started in NetworkStream.BeginWrite(Byte[], int, int, AsyncCallback, object).

Before calling NetworkStream.BeginWrite(Byte[], int, int, AsyncCallback, object), you need to create a callback method that implements the AsyncCallback delegate. This callback method executes in a separate thread and is called by the system after NetworkStream.BeginWrite(Byte[], int, int, AsyncCallback, object) returns. The callback method must accept the IAsyncResult returned from the NetworkStream.BeginWrite(Byte[], int, int, AsyncCallback, object) method as a parameter.

Within the callback method, call the IAsyncResult.AsyncState property of the IAsyncResult parameter to obtain the System.Net.Sockets.NetworkStream. After obtaining the System.Net.Sockets.NetworkStream, you can call the NetworkStream.EndWrite(IAsyncResult) method to successfully complete the send operation and return the number of bytes sent.

The NetworkStream.EndWrite(IAsyncResult) method blocks until the requested number of bytes are sent.

Note:

If you receive an System.IO.IOException, check the Exception.InnerException property to determine if it was caused by a System.Net.Sockets.SocketException. If so, use the SocketException.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.

Example

For an outline of an asynchronous operation, see the Socket.BeginAccept(AsyncCallback, object) method. For the complete example, see the System.Net.Sockets.Socket class overview.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0