NetworkStream.Read troubles

T

Terry Olsen

Need a little help with reading from a network stream. I'm downloading
articles from an NNTP server. The end of an article is signified by
<CRLF>.<CRLF>

For all the talking back & forth, I used a regular StreamReader &
StreamWriter. This works great except when i'm downloading a yEncoded binary
article. So I have to use the NetworkStream object. However, it's not so
easy now to know when I've hit the end of the article. Here's the code I'm
trying to use:

Dim buf(1024) As Byte
Dim fs As New FileStream(FileName, FileMode.Create)
Dim ns As NetworkStream = _NNTPClient.GetStream
Do
Dim x As Integer = ns.Read(buf, 0, buf.Length)
fs.Write(buf, 0, x)
Loop While ns.DataAvailable = True
fs.Close()

I never get more than 2 or 3 Kb's of an article before the loop ends because
DataAvailable is False. I found that if I put a messagebox in the loop and
click on it each time it pops up, I get the entire article. I don't want to
put an artificial delay in the loop (such as a sleep statement), but I can't
figure out how to get all of the article.

Is there a method for scanning the incoming data for the <CRLF>.<CRLF>
pattern?

..

..
 
J

Jack Jackson

Need a little help with reading from a network stream. I'm downloading
articles from an NNTP server. The end of an article is signified by
<CRLF>.<CRLF>

For all the talking back & forth, I used a regular StreamReader &
StreamWriter. This works great except when i'm downloading a yEncoded binary
article. So I have to use the NetworkStream object. However, it's not so
easy now to know when I've hit the end of the article. Here's the code I'm
trying to use:

Dim buf(1024) As Byte
Dim fs As New FileStream(FileName, FileMode.Create)
Dim ns As NetworkStream = _NNTPClient.GetStream
Do
Dim x As Integer = ns.Read(buf, 0, buf.Length)
fs.Write(buf, 0, x)
Loop While ns.DataAvailable = True
fs.Close()

I never get more than 2 or 3 Kb's of an article before the loop ends because
DataAvailable is False. I found that if I put a messagebox in the loop and
click on it each time it pops up, I get the entire article. I don't want to
put an artificial delay in the loop (such as a sleep statement), but I can't
figure out how to get all of the article.

Is there a method for scanning the incoming data for the <CRLF>.<CRLF>
pattern?

DataAvailable tells you whether or not data has been received by the
TCP stack that you haven't read yet, it doesn't tell you anything
about whether or not more data will arrive in the future.

There is a thread about this here:
http://www.dotnet247.com/247reference/msgs/52/262200.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top