Re: Reading network socket stream, slow connection

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Fri, 06 Apr 2007 21:18:38 -0700, Sir Psycho <totalharmonicdistortion@xxxxxxxxxxx> wrote:

Hi,

For some reason, when i step over this code, it returns the full byte
stream im expecting from the server, however when I let it run with no
intervention, it only seems to grab a small chunk on the stream.

If you step over it in the debugger, your program is paused long enough for the network driver to receive all of the data you're expecting. When your program runs without intervention, it reads data faster than it can be sent over the network, and so only reads a little bit at a time (whatever's been received by the network driver since the last time you read some data).

Standard mistake most beginning network programmers who haven't studied the documentation make: stream-oriented protocols (like TCP) do not preserve data boundaries imposed by the sender. The bytes are guaranteed to be in the right order, but they may be grouped in any manner. For any given receive operation in your code, you might only receive a single byte, you might read everything all at once (or at least up to whatever the buffer you provided to read into), or anything in between.

What am I doing wrong? There is more code than this, but this is the
problem code.

It's not clear from the code you posted that you are doing anything wrong. It depends on what you do the byte array after you receive it. The behavior you're seeing is by design, so as long as you properly accumulate the received data until you have enough to accomplish whatever processing you're going to do with the data, that's fine.

If your code assumes that it will receive exactly the same thing that the sender sent, all of the bytes all at once, then that's a mistake and you need to write your code to properly receive the data until you have everything you expect for processing.

Pete
.



Relevant Pages

  • Re: .NET to Delphi 7
    ... get it to work with Indy HTTP? ... From the VB code you posted, it looks like the server is expecting the file to be included as the payload of a POST request. ... If you want to receive the server's response in another stream, for instance, use the overload that takes a string and two streams as arguments, instead of the one I used above, which only takes one stream. ...
    (alt.comp.lang.borland-delphi)
  • Re: LJ missing?
    ... Well that's a rather different response from the stream of profanity I ... was expecting! ... He's gone soft in his old age. ...
    (uk.misc)
  • Re: XMLDocument and NetworkStream (VB.NET 2005)
    ... With this code the "Load" never returns. ... Are you expecting the ... connection to be closed after the whole document has been written? ... If the stream never ends, ...
    (microsoft.public.dotnet.framework)
  • Re: Erich, we hope you are having a good day :)
    ... I was going to offer you a private stream tomorrow, ... To be honest, I wasn't expecting it to be much of a match, and when one of ... Sky did show the last two games of the match after the Roddick match, ...
    (rec.sport.tennis)