Hi,
On the desktop framework I never really stopped to think about
buffer sizes when receiving data using Sockets, with all the memory… who cares.
But with the devices it’s a completely different issue.
So I’m wondering how most people deal with this, if there are any best practices etc…
For example, my code here is probably absolutely no good:
client = server.Accept();
client.ReceiveTimeout = 120000;
byte[] buffer = new byte[5000];
try
{
var count = 1;
while (count > 0)
{
count = client.Receive(buffer);
if (OnDataReceived != null)
OnDataReceived(Utility.ExtractRangeFromArray(buffer, 0, count));
}
}
//end of snippet
First I create a buffer of 5000 bytes, even if far less data will be received.
Second if I receive more than 5000 bytes the code fails as well
A half solution would be to make the buffer bigger, but it’s not an optimal solution either…
What’s your take on this?
Perhaps I’m doing this completely wrong?