Read from TCP Stream greater then buffer size

G

Guest

I'm pretty new to VB .net and Network coding, what I'm trying to do is
interact with a NNTP server to retrieve a list of news groups.

When I run thru' debug I basically get what I'm after, when I run without
debug the results vary - which to me suggests it's some form of sync problem
with my 'writes and reads'.

I've set up a connection to a server and have sent a 'List' command to get a
'group list', I've set up my connection buffer at 32k, I'm assuming I'm
blocking on the stream read, the question is how to I control receiving a
complete returned message - which could be 500k. I've tried various do-loop
controls, some based on MSDN examples such as 'Loop While
NStream.DataAvailable' and the attached which I'd hoped would loop until a
message which was less then the complete buffer was returned - but it all
seems hit and miss.

thanks

Dim numberOfBytesRead As Integer = 0
Dim sb As New StringBuilder

Do
numberOfBytesRead = NStream.Read(bytes, 0, CInt
Connection.ReceiveBufferSize))
If numberOfBytesRead = 0 Then
Exit Do
End If
sb.Append(Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead))
totallength = sb.Length
Loop Until numberOfBytesRead < Connection.ReceiveBufferSize
 
K

Ken Tucker [MVP]

Hi,

Maybe this will help.
http://www.programmersheaven.com/2/Art_CSharp_4

Ken
-------------------
I'm pretty new to VB .net and Network coding, what I'm trying to do is
interact with a NNTP server to retrieve a list of news groups.

When I run thru' debug I basically get what I'm after, when I run without
debug the results vary - which to me suggests it's some form of sync problem
with my 'writes and reads'.

I've set up a connection to a server and have sent a 'List' command to get a
'group list', I've set up my connection buffer at 32k, I'm assuming I'm
blocking on the stream read, the question is how to I control receiving a
complete returned message - which could be 500k. I've tried various do-loop
controls, some based on MSDN examples such as 'Loop While
NStream.DataAvailable' and the attached which I'd hoped would loop until a
message which was less then the complete buffer was returned - but it all
seems hit and miss.

thanks

Dim numberOfBytesRead As Integer = 0
Dim sb As New StringBuilder

Do
numberOfBytesRead = NStream.Read(bytes, 0, CInt
Connection.ReceiveBufferSize))
If numberOfBytesRead = 0 Then
Exit Do
End If
sb.Append(Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead))
totallength = sb.Length
Loop Until numberOfBytesRead < Connection.ReceiveBufferSize
 
G

Guest

Thanks Ken, I had been basing my code on this article.

I've ended up with a simple loop

Dim sb As New StringBuilder

While NStream.DataAvailable
Try
numberOfBytesRead = NStream.Read(bytes, 0,
CInt(Connection.ReceiveBufferSize))
sb.Append(Encoding.ASCII.GetString(bytes, 0,
numberOfBytesRead))
Catch ex As Exception
Exit While
End Try
End While

If I run it with breakspoint in debug and step thru the loop iterations I
get a full set of groups returned - I keep a byte count which comes back with
over 100k data returned. If I run it without breakpoints I get a partial list
back with a total byte count of 5040. I've varied by buffersize but this
appears to have no impact.

The fact it works under debug probably means the code is correct but it
appears to be a timing/syncing thing.

I've built the example from the URL above (and from another) and they work -
but they are in c# - is it a VB thing?
 

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