I've made a program which is sending images over TCP sockets, it can be running for a while then always on the server side it has an error deserializing the stream.
Additional information: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
The full function which is causing the error is:
private void handleClientThread(TcpClient tcpClient)
{
while (true)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
pictureBox1.Image = (Image)binaryFormatter.Deserialize(tcpClient.GetStream()); // this line is the one throwing an exception
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
The client is sending everything correctly:
private Image TakePicture()
{
Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmpScreenCapture);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
return bmpScreenCapture;
}
private void SendPicture()
{
Image clientPicture = TakePicture();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(sharerClient.GetStream(), clientPicture);
}
Could anyone help me with this issue or help me send the images over TCP sockets without it throwing an exceptions.