Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

NetworkStream::EndWrite Method (IAsyncResult^)

 

Handles the end of an asynchronous write.

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

public:
virtual void EndWrite(
	IAsyncResult^ asyncResult
) override

Parameters

asyncResult
Type: System::IAsyncResult^

The IAsyncResult that represents the asynchronous call.

Exception Condition
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.

System_CAPS_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
static void myWriteCallBack( IAsyncResult^ ar )
{
   NetworkStream^ myNetworkStream = safe_cast<NetworkStream^>(ar->AsyncState);
   myNetworkStream->EndWrite( ar );
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2017 Microsoft