Re: Fundamentals question, is this how it works?




"Daniel" <DanielV@xxxxxxxxxxxxxxxx> wrote in message
news:uDfBVwCrGHA.4864@xxxxxxxxxxxxxxxxxxxxxxx
Hey

With a tcp stream socket what happens when it is reading say 4000bytes and
495 bytes come in?

I am finding when i add a delay in my reading i always read my data
correctly. If i let it run at full speed it gets stuck. I am using
asynchornous sockets.

On my understanding is this right, say i have a receive buffer of 1024
bytes
and i send 2048 bytes, then straight after i send 400 bytes:

1) Client receives 1024 bytes of data from original 2048 sent, and
processes
it
2)After client reads all the data it receives the next 1024 bytes, and
processes it
3) then it reads the first 400 bytes and processes it.

Am i right in that it queues up that way?

No, this is not the way it works.

TCP is a stream-based protocol, which means that it ignores any attempt (on
the sending side) to somehow group the sent data into "messages" or
"packets" or whatever you want to call them. If the sending side sends 2048
bytes, then (assuming a buffer large enough) the receiving side might get
all 2048 bytes in one call to recv(), or it might need many calls to recv()
before all 2048 bytes are sent. If the sending side sends 2048 bytes and
then sends another 400 bytes, then the receiving side might get all 2448
bytes in one call to recv() or (again) it might need many calls to recv()
before all 2448 bytes are received. Moreover, the groupings by which these
bytes are received will generally ignore the fact that 2048 bytes were sent
first followed by 400 bytes, so a first call to recv() might get 1531 bytes
(arbitrary number, just for example's sake), a second call to recv() might
get 814 bytes, and a third call might get the remaining 103 bytes.

The only thing that TCP guarantees is that the bytes will be received in the
correct order. TCP does not guarantee that the bytes will be received in
the same groupings as when they were sent. In fact, in a degenerative case
that probably would never occur in practice (but is still theoretically
possible), only one single byte at a time might be retrieved by each call to
recv().



And why if i slow down the reading
does it become reliable but if i let it read at full speed it gets stuck.
When i say stuck i mean it ignores new data coming in. So it does this,
but
this is accurate to my real situation

1) Reads 425 bytes
2) now no longer reads any new data

Is it possible it is trying to read too quick? since it is in a loop
anyway
surely it has to finish reading the first before it can continue anyhow?





Generally, situations like this are caused by code that assumes that TCP
preserves groupings. TCP itself is not stuck. However, because of the
mistaken assumption, the code has encountered an unexpected situation, such
as looking for a NULL terminator in c-style string, and then discarding
everything after that. This is a mistake because TCP might have delivered
part of the next string, such that the code discards valid data.


.



Relevant Pages

  • Re: ioctl(2) FIONREAD
    ... It can be obsolete ... > arrive between the ioctland the recv(), but I should know that I've ... What's the difference in receiving all in one go, ... Keep in mind that TCP is a stream though, ...
    (comp.unix.programmer)
  • Re: TCP question
    ... It's a stream. ... by its nature it behaves more like a stream at the TCP level. ... the code you have for receiving the packets ... Consecutive packets can even take different routes from sender ...
    (microsoft.public.vb.general.discussion)
  • Re: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug
    ... Your first read is receiving 0 bytes? ... Your async socket listens and receives 500 bytes (your total buffer size ... But the data remains on the socket, until such time as it IS ... With a stream-oriented socket (eg TCP), ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: send & MSG_DONTWAIT
    ... TCP transport and neither has the receiving application. ... That's why an "all or nothing" non-blocking send doesn't exist. ... the last 'complete frame' has been received and then, ...
    (comp.unix.programmer)
  • Re: The best way to wait for something to happen
    ... The Winsock Control has a "ConnectionRequest" Event which is what the sample ... I'm still a little hazy on how to do this with TCP. ... TCP does not require a client and a server any more than UDP does. ... the receiving workstation will receive all of the data but ...
    (microsoft.public.fox.programmer.exchange)

Loading