Export (0) Print
Expand All

NetworkStream.EndRead Method

Handles the end of an asynchronous read.

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

public override int EndRead (
	IAsyncResult asyncResult
)
public int EndRead (
	IAsyncResult asyncResult
)
public override function EndRead (
	asyncResult : IAsyncResult
) : int

Parameters

asyncResult

An IAsyncResult that represents an asynchronous call.

Return Value

The number of bytes read from the NetworkStream.

Exception typeCondition

ArgumentException

asyncResult is a null reference (Nothing in Visual Basic).

IOException

The underlying Socket is closed.

-or-

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

ObjectDisposedException

The NetworkStream is closed.

The EndRead method completes the asynchronous read operation started in the BeginRead method.

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

Within the callback method, call the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginRead method. Extract the receiving NetworkStream from this state object. After obtaining the NetworkStream, call the EndRead method to successfully complete the read operation and return the number of bytes read.

The EndRead method blocks until data is available. The EndRead method reads as much data as is available up to the number of bytes specified in the size parameter of the BeginRead method. If the remote host shuts down the Socket connection and all available data has been received, the EndRead method completes immediately and returns zero bytes.

To obtain the received data, call the AsyncState property of the IAsyncResult, and extract the buffer contained in the resulting state object.

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, myReadCallback is provided to BeginRead as the callback method. EndRead is implemented in myReadCallback to complete the asynchronous read call started by BeginRead.


public static void myReadCallBack(IAsyncResult ar ){

    NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
    byte[] myReadBuffer = new byte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = 
        String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));    
    
    // message received may be larger than buffer size so loop through until you have it all.
    while(myNetworkStream.DataAvailable){
    	
        myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, 
        	                                       new AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack), 
        	                                       myNetworkStream);  

    }

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                myCompleteMessage);
}


public static void MyReadCallBack(IAsyncResult ar)
{
    NetworkStream myNetworkStream = (NetworkStream)ar.get_AsyncState();
    ubyte myReadBuffer[] = new ubyte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = String.Concat(myCompleteMessage, 
        Encoding.get_ASCII().GetString(myReadBuffer, 0, numberOfBytesRead));
    // message received may be larger than buffer size so loop through
    // until you have it all.
    while (myNetworkStream.get_DataAvailable()) {
        myNetworkStream.BeginRead(myReadBuffer, 0,
            myReadBuffer.get_Length(),
            new AsyncCallback(NetworkStreamASyncSendReceive.MyReadCallBack),
            myNetworkStream);
    }
    // Print out the received message to the console.
    Console.WriteLine(("You received the following message : " 
        + myCompleteMessage));
} //MyReadCallBack

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft