Export (0) Print
Expand All

NetworkStream.EndWrite Method

Handles the end of an asynchronous write.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public override void EndWrite(
	IAsyncResult asyncResult
)

Parameters

asyncResult
Type: System.IAsyncResult

The IAsyncResult that represents the asynchronous call.

ExceptionCondition
ArgumentNullException

The asyncResult parameter is null.

IOException

The underlying Socket is closed.

-or-

An error occurred while writing to the network.

-or-

An error occurred when accessing the socket. See the Remarks section for more information.

ObjectDisposedException

The NetworkStream is closed.

EndWrite completes the asynchronous send operation started in BeginWrite.

Before calling BeginWrite, 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 BeginWrite returns. The callback method must accept the IAsyncResult returned from the BeginWrite method as a parameter.

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

The EndWrite method blocks until the requested number of bytes are sent.

NoteNote

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 in MSDN for a detailed description of the error.

In the following code example, myWriteCallback is provided to BeginWrite as the callback method. EndWrite is implemented in myWriteCallback to complete the asynchronous write call started by BeginWrite.

// Example of EndWrite 
public static void myWriteCallBack(IAsyncResult ar){

     NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
     myNetworkStream.EndWrite(ar);
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft