Export (0) Print
Expand All

NetworkStream.EndWrite Method

Handles the end of an asynchronous write.

[Visual Basic]
Overrides Public Sub EndWrite( _
   ByVal asyncResult As IAsyncResult _
)
[C#]
public override void EndWrite(
 IAsyncResult asyncResult
);
[C++]
public: void EndWrite(
 IAsyncResult* asyncResult
);
[JScript]
public override function EndWrite(
   asyncResult : IAsyncResult
);

Parameters

asyncResult
The IAsyncResult representing the asynchronous call.

Exceptions

Exception Type Condition
ArgumentNullException asyncResult is a null reference (Nothing in Visual Basic).
IOException The underlying Socket is closed.

-or-

An error occurs while writing to the network.

ObjectDisposedException The NetworkStream is closed.
IOException An error occurred when accessing the socket. See the Remarks section for more information.

Remarks

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 method 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 will block until the requested number of bytes are sent.

Note   If you receive a IOException check the InnerException property to determine if it was caused by a SocketException. If so, use ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Example

[Visual Basic, C#, C++] In the following example, myWriteCallback is provided to BeginWrite as the callback method. EndWrite is implemented in myWriteCallback to complete the asynchronous write call started by BeginWrite.

[Visual Basic] 
Public Shared Sub myWriteCallBack(ar As IAsyncResult)
   
   Dim myNetworkStream As NetworkStream = CType(ar.AsyncState, NetworkStream)
   myNetworkStream.EndWrite(ar)
End Sub 'myWriteCallBack



[C#] 
public static void myWriteCallBack(IAsyncResult ar){

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


[C++] 
static void myWriteCallBack(IAsyncResult* ar) {
    NetworkStream* myNetworkStream = __try_cast<NetworkStream*>(ar->AsyncState);
    myNetworkStream->EndWrite(ar);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

NetworkStream Class | NetworkStream Members | System.Net.Sockets Namespace | BeginWrite | Including Asynchronous Calls

Show:
© 2014 Microsoft