NetworkStream.EndRead Method
Handles the end of an asynchronous read.
Assembly: System (in System.dll)
Parameters
- asyncResult
- Type: System.IAsyncResult
An IAsyncResult that represents an asynchronous call.
| Exception | Condition |
|---|---|
| ArgumentException |
The asyncResult parameter is null. |
| 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.
Note
|
|---|
|
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.
// Example of EndRead, DataAvailable and 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); }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Personally, I would define a small local class to represent the AsyncState object. This class contains the byte[] buffer which will hold the received data, and also a reference to the NetworkStream object. For example:
public class ReadStateObject() {
public NetworkStream Stream { get; set; }
public byte[] ReadBuffer;
public ReadStateObject(NetworkStream _stream, int bufferSize) {
Stream = _stream;
ReadBuffer = new byte[bufferSize];
}
}
Following on from this, the BeginRead() method can be called as such:
ReadStateObject stateObject = new ReadStateObject(Stream, 1024);
Stream.BeginRead(stateObject.ReadBuffer, 0, stateObject.ReadBuffer.Length, new AsyncCallback(ReadCallBack), stateObject);
And finally, the callback, in which the state object (containing the stream and the [now read] bytes) as follows:
void ReadCallBack(IAsyncResult ar) {
ReadStateObject stateObject = (ReadStateObject)ar.AsyncState;
NetworkStream myNetworkStream = stateObject.Stream;
String strInput = string.Empty;
int numberOfBytesRead;
try {
numberOfBytesRead = myNetworkStream.EndRead(ar);
strInput = Encoding.UTF8.GetString(stateObject.ReadBuffer, 0, numberOfBytesRead);
// Message received may be larger than buffer size: if so loop to get the lot
if (myNetworkStream.DataAvailable) {
byte[] overspill = new byte[1024];
while (myNetworkStream.DataAvailable) {
numberOfBytesRead = myNetworkStream.Read(overspill, 0, overspill.Length);
strInput = String.Concat(strInput, Encoding.UTF8.GetString(overspill, 0, numberOfBytesRead));
}
}
} catch (ObjectDisposedException) { // Do nothing - we've closed the stream
return; // will also execute finally{} below to release calling thread
} catch (Exception ex) {
SendDebug("ConnectionHandler: Could not read from stream:");
} finally {
// Free up the main read loop thread to process more read requests
readComplete.Set();
}
// Process the string received
ReadDataCompleted(strInput);
}
Just some suggested code.
- 2/17/2011
- Carlosp_uk
- 5/10/2012
- Jason OB
"To obtain the received data, call the AsyncState property of the IAsyncResult, and extract the buffer contained in the resulting state object."
This is incorrect and should be removed, the AsyncState reflects the argument passed in BeginRead (as noted correctly in the documentation)
- 4/8/2011
- Jelle Hissink - CodeWise
while (myNetworkStream.DataAvailable)
{
myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length,
new AsyncCallback(myReadCallBack),
myNetworkStream);
}
Sorry, but I fail to see how this is going to work - in fact, I'd go so far as to say it's not.
Surely, the string 'myCompleteMessage' is local to the method myReadCallBack.
Therefore, every time the function calls itself, it will re-initiate its own local version of 'myCompleteMessage', so none of the string concatenation intended will take place.
Correct me if I'm wrong!
- 2/17/2011
- Carlosp_uk
while(myNetworkStream.DataAvailable){"
it does not work if you run a client and a server code on different computers. (But works if you run a client and a server code on the same computer).
- 7/28/2010
- vozmen
Note