The data in question is a PNG file prefixed by it’s size as an int.
-Sending:
ns.Write(BitConverter.GetBytes((int)data.Length),0,4);
ns.Write(data, 0, data.Length);
-Reading:
byte[] sizearray = new byte[4];
ns.Read(sizearray, 0, 4);
int dataSize = BitConverter.ToInt32(sizearray,0);
byte[] data = new byte[dataSize];
ns.Read(data, 0, dataSize);
The recieved data is then saved to a file. I’ve also tried this with BeginRead/EndRead with the same result.
The issue is, while this works for most smaller images, it doesn’t receive an image which is more then a few KB. The dataSize reads correctly, but after a few thousand bytes each time (~2900), the rest of the received data is 0. Example
Have I overlooked something, like a limit on how much can be sent at once?
You’re ignoring the return value of
Read. Don’t do that.Readdoesn’t wait until it’s read all of the data you requested. You should read in a loop until you’ve read everything you need to:In theory, you need to do the same even when reading
dataSize, although in practice I doubt whether you’ll receive fewer than 4 bytes in one read.You might want to use a
BinaryReaderwrapped around the stream – then you could useReadInt32andReadBytes, whereReadByteswill do the looping for you. (You’d still need to check the return value, but it would be simpler than doing it yourself.)