Re: Reading network socket stream, slow connection
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 06 Apr 2007 21:37:52 -0700
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
.
- Follow-Ups:
- Re: Reading network socket stream, slow connection
- From: Sir Psycho
- Re: Reading network socket stream, slow connection
- References:
- Reading network socket stream, slow connection
- From: Sir Psycho
- Reading network socket stream, slow connection
- Prev by Date: Re: C# Excel Add-in works on some machines, but then not others
- Next by Date: Re: C# Excel Add-in works on some machines, but then not others
- Previous by thread: Reading network socket stream, slow connection
- Next by thread: Re: Reading network socket stream, slow connection
- Index(es):
Relevant Pages
|