1
votes

I am using the NetworkStream of a TcpClient to send bytes from a .NET server to a Silverlight Client that receives them via a Socket. Sending from the client is done via Socket.SendAsync.

My question is, what is the minimum amount of bytes I can expect to be received "in one go" on both sides, without the need to send the message length too and put several byte messages together by myself.

Thanks, Andrej

2
1 byte - Always send the length or use a termination character - CodesInChaos

2 Answers

4
votes

You absolutely should send the message length. Network streams are precisely that - streams of information. Basically there are three ways of recognising the end of a message:

  • Prefix the data with the length (don't forget that it's at least theoretically possible that you won't even get all of the length data in one packet)
  • Use a message delimiter/terminator
  • Only send one message each way per connection, and close the connection afterwards
3
votes

It depends on your network settings, but the default length for network nodes is 1500 bytes and most modems take something off that. So most homes have 1460 bytes packet size.

The optimum size for your situation can be calculated

But people can always have their own settings, so there's no guarantee that you get the optimal packet size for all clients.