network read question

P

parez

Hi,

When does the socket (server) know when to stop reading.

e.g.

if i have a buffer = 25K

and do networkStream.write twice.. what will the server read? 25k or
50K?

And should i send a message length for every message i send? and make
the server read specified number of bytes?


networkStream.Write(buffer, 0, buffer.Length);


networkStream.Write(buffer, 0, buffer.Length);


**********************Server Code ***********************************
Socket newsock = new
Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

newsock.Bind(ipep);
newsock.Listen(10);


Socket client = newsock.Accept();
data = new byte[64000];

recv = client.Receive(data);

**********************************************
 
P

parez

If you write 50K, the server will eventually read 50K. However, there is
no way to predict in advance how the server will read that 50K. It could
read a single byte 50 thousand times, it could read 5 bytes ten thousand
times, it could read 50K once, it could read 10K, 10K, 25K, then 5K, it
could...

Any possible permutation is possible.


With TCP (which is what NetworkStream implies), you _must_ provide some
mechanism to allow the recipient to know how long the transmission is.

In the simplest case, the transmission is "complete" when the connection
is closed. For some protocols, that's sufficient. For higher-performance
protocols, where you want to leave the connection open between your
logical messages, you need to incorporate some means of delimiting
messages. Preceding each logical message with a byte count is indeed a
legitimate and common way to accomplish that.

Pete

Thanks..


I have a situation where i sending a approx 50k message. the server
is reading it into a 128 k buffer. but it only 2k (most of the times)
and the number varies.. the server program is written so that it
quits reading after the first read
(max size of the message is apprx 65K). So i will have to send the
first x bytes as message length?



Coming back to the original question, there is no way of certainly
knowing how many bytes will the server read in one go...
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a situation where i sending  a approx 50k message. the server
is reading it into a 128 k buffer.  but it only 2k (most of the times)
and the number varies.. the server program is written  so that it
quits reading after the first read

Do you have the code of the server?
Remember that even as you say read X bytes you will get only the
bytes that were read already. The server usually works in a loop.
(max size of the message  is apprx 65K). So i will have to send the
first x bytes as message length?

Does the server support that?
If not the length will become part of the data.
Coming back to the original  question,  there is no way of certainly
knowing  how many bytes will the server read in one go...

Nop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top