By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Async NetworkStream: Problem With receiving large chunks of data

P: n/a

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

Jul 16 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mon, 16 Jul 2007 05:33:34 -0700, Rogier <Ro********@gmail.comwrote:
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
Jul 16 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.