Async NetworkStream: Problem With receiving large chunks of data

R

Rogier

Hello,

I'm Writing my own NZb download App, using Async Network Streams en
Callback delegates. The Problem is that if I Want tot Receive Large
chunks of data (for instance the LIST NEWSGROUPS Response). I do not get
the Data at once.

Here is a piece of my source code:

I call the NetworkStream.ReadBegin as follows:
(ReadCallBack is the Call Back delegate)


byte[] ReadBuffer = new byte[100000];
stream.BeginRead(ReadBuffer, 0,
ReadBuffer.Length,ReadCallback,ReadBuffer);


And here is the Callback function:

protected void Stream_Read(IAsyncResult result)
{
int BytesRead = 0;
System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();

try
{
BytesRead = stream.EndRead(result);
}
catch (Exception e)
{

}
richTextBox1.Text +=
encoding.GetString((byte[])result.AsyncState, 0, BytesRead);

}


To get the full Response I have tot call the BeginRead Methode multiple
times, Currently I have to do it manually because I cant find a way to
check programmaticaly whether to call it again. Does anyone have A
solution for this problem ?

Thanks

Rogier
 
P

Peter Duniho

I'm Writing my own NZb download App, using Async Network Streams en
Callback delegates. The Problem is that if I Want tot Receive Large
chunks of data (for instance the LIST NEWSGROUPS Response). I do not get
the Data at once.

This is by design. A network stream is going to return data received as
soon as it has _some_ data, and necessarily wait until it has _all_ data.
This is true whether you use synchronous or asynchronous methods (other
than ReadToEnd(), of course).

You are required to use whatever information is available in the protocol
to indicate the length of the data, and read repeatedly until you have
enough data to process. Depending on the protocol, this could mean
receive a count of bytes in advance, looking for a delimiter at the end of
the data, or simply reading until the connection is closed.

Pete
 
Top