Re: Read from TCP Stream greater then buffer size

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Rob (Rob_at_discussions.microsoft.com)
Date: 11/28/04


Date: Sun, 28 Nov 2004 06:35:01 -0800

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?

"Ken Tucker [MVP]" wrote:

> Hi,
>
> Maybe this will help.
> http://www.programmersheaven.com/2/Art_CSharp_4
>
> Ken
> -------------------
> "Rob" <Rob@discussions.microsoft.com> wrote in message
> news:85A61B6D-A8F6-48E7-B012-B9B68F0B90E8@microsoft.com...
> 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
>
>
>
>
>



Relevant Pages

  • Re: Read from TCP Stream greater then buffer size
    ... interact with a NNTP server to retrieve a list of news groups. ... the question is how to I control receiving a ... complete returned message - which could be 500k. ... some based on MSDN examples such as 'Loop While ...
    (microsoft.public.dotnet.languages.vb)
  • interesting error
    ... the c++ dll that contains the meat of the main loop. ... So I have a timer set ... Debug Error! ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Frame-based exception handling problem on Server 2008
    ... To debug debug the issue, ... The endless loop occurs as soon as the PC is at ... typedef struct _exception_list ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Bug with Chart.Name?
    ... .Chartobjects.Count loop ... Dim shp As Shape, chtObj As ChartObject, cht As Chart ... processed as you can see in the debug output, ...
    (microsoft.public.excel.charting)
  • Re: Debugging problems
    ... > Violation exceptions in the debug window. ... > And the strange thing is when I added a MessageBox to the loop to find out ... > I'm guessing it's a memory corruption problem. ... > impossible to find the problem by sight (as Debug mode won't help). ...
    (microsoft.public.vc.language)