Re: recv() - How do you know the length of buffer that you are going to receive?



Steven wrote:
Hi everyone,

I'm puzzled.
How do you usually define the length to be given for recv()? Since you
do not know what is the length of the buffer you are going to receive?
You must have an application protocol.
It could be as simple as "read everything until the stream is closed",
in which case you divide it up in suitable chunks and grow up
a buffer in memory, or write to a file as you go.
Or, you could send the length of each part/message/etc. first,
as e.g. 4 octets. Then you can read 4 octets which represent the length
of the data that is about to come.

Remember, if you're using TCP, it's a stream write bundaries at one end
of the connection are NOT preserved. You can't know how much data the
other end sent using just one write/send call, and TCP can split/merge
those at will.
.



Relevant Pages

  • Re: Algorithm for reading from continuous stream
    ... > I don't have much experience with reading from continuous stream (I ... > over bytes and the end of the buffer. ... TCP will give you a stream input. ... Repeat until request is satisfied. ...
    (comp.programming)
  • Re: Fundamentals question, is this how it works?
    ... That is what i thought i was saying that it receives it all in a stream ... receving the buffer size each time. ... receiving that many bytes i then break and wait for the next set of data to ... TCP is a stream-based protocol, which means that it ignores any attempt ...
    (microsoft.public.win32.programmer.networks)
  • Re: Windows Sockets (TCPClient) problem.
    ... What TCP doesn't guarantee is the size of data. ... > part equals the buffer size and send the parts sequentialy. ... > the listener is detecting a stream to be read once every two times the ... > sender sends a stream. ...
    (microsoft.public.dotnet.academic)
  • Re: About TDI
    ... TCP is a continuous byte stream of information. ... When a TCP buffer is sent it ... > but at TDI server I received three packets. ...
    (microsoft.public.development.device.drivers)
  • Re: I/O buffering
    ... StreamReader also buffers data? ... so they buffer as well. ... Neither StreamReader nor StreamWriter inherit from FileStream. ... What StreamReader and StreamWriter _do_ use is any Stream instance. ...
    (microsoft.public.dotnet.languages.csharp)