I have the following code that sends data:
writer is a NetworkStreamWriter instance.
img is an Image instance.
writer.AutoFlush = true;
BinaryWriter bWriter = new BinaryWriter(writer.BaseStream);
MemoryStream ms = new MemoryStream();
byte buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
then, on the client side, I have a receiving code:
var reader = new StreamReader(stream);
string message = await reader.ReadLineAsync();
BinaryReader breader = new BinaryReader(stream);
int ctBytes = breader.ReadInt32();
MemoryStream ms = new MemoryStream(breader.ReadBytes(ctBytes));
Image img = Image.FromStream(ms);
Bitmap bmp = new Bitmap(img, (int)System.Windows.SystemParameters.PrimaryScreenWidth, (int)System.Windows.SystemParameters.PrimaryScreenHeight);
the sending code runs every 40 miliseconds, and the receiving code runs inside a loop asynchronously (works when the "picture" string is sent)
but the receiving code works only for about 4 iterations, afterwards the ctBytes becomes negative and an exception is being throwed (you can't read a negative number of bytes).
I don't know what is wrong.