F
FLASHCODR
Guest
I'm using the following code (reference) to receive realtime screenshots from client and he working fine except when occurs some problem inside this following part and whole application (Server) is locked including the system (Windows). What i can make to solve this?
public event ReceivedMessageEventHandler receivedMessage;
public delegate void ReceivedImageEventHandler(byte[] image);
public event ReceivedImageEventHandler receivedImage;
...
receivedImage += ReceivedImageEventHandler(serverReceivedImage);
...
private void serverReceivedImage(byte[] image)
{
try
{
byte[] newImage = new byte[image.Length - 6];
Array.Copy(image, 6, newImage, 0, newImage.Length);
using (var stream = new MemoryStream(newImage))
{
using (var msInner = new MemoryStream())
{
stream.Seek(2, SeekOrigin.Begin);
using (DeflateStream z = new DeflateStream(stream, CompressionMode.Decompress))
{
z.CopyTo(msInner); // << Error occurs commonly here on stream decompression and the application crashes.
}
msInner.Seek(0, SeekOrigin.Begin);
var bitmap = new Bitmap(msInner);
Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
}
}
}
catch (Exception ex)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
//Console.WriteLine(("serverReceivedImage() Error: " + ex.ToString()));
}
}
The part that process the great part of data is running inside a separated thread eg:
new Thread(() =>
{
while (true)
{
var message = String.Empty;
var lengthData = new byte[4];
var lengthBytesRead = 0;
while (lengthBytesRead < lengthData.Length)
{
var read = clientSock.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read <= 0) return;
lengthBytesRead += read;
}
if (BitConverter.IsLittleEndian)
Array.Reverse(lengthData);
var length = BitConverter.ToInt32(lengthData, 0);
if (length > 0)
{
var data = new byte[length];
var bytesRead = 0;
while (bytesRead < data.Length)
{
var read = clientSock.Receive(data, bytesRead, data.Length - bytesRead, SocketFlags.None);
if (read <= 0) return;
bytesRead += read;
message += Encoding.UTF8.GetString(data, 0, read);
}
if (message.Contains("CONNECTION") || message.Contains("PONG") || message.Contains("PROC"))
receivedMessage(client, message);
else if (message.Contains("SCREEN"))
receivedImage(data);
}
}
}).Start();
Continue reading...
public event ReceivedMessageEventHandler receivedMessage;
public delegate void ReceivedImageEventHandler(byte[] image);
public event ReceivedImageEventHandler receivedImage;
...
receivedImage += ReceivedImageEventHandler(serverReceivedImage);
...
private void serverReceivedImage(byte[] image)
{
try
{
byte[] newImage = new byte[image.Length - 6];
Array.Copy(image, 6, newImage, 0, newImage.Length);
using (var stream = new MemoryStream(newImage))
{
using (var msInner = new MemoryStream())
{
stream.Seek(2, SeekOrigin.Begin);
using (DeflateStream z = new DeflateStream(stream, CompressionMode.Decompress))
{
z.CopyTo(msInner); // << Error occurs commonly here on stream decompression and the application crashes.
}
msInner.Seek(0, SeekOrigin.Begin);
var bitmap = new Bitmap(msInner);
Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
}
}
}
catch (Exception ex)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
//Console.WriteLine(("serverReceivedImage() Error: " + ex.ToString()));
}
}
The part that process the great part of data is running inside a separated thread eg:
new Thread(() =>
{
while (true)
{
var message = String.Empty;
var lengthData = new byte[4];
var lengthBytesRead = 0;
while (lengthBytesRead < lengthData.Length)
{
var read = clientSock.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read <= 0) return;
lengthBytesRead += read;
}
if (BitConverter.IsLittleEndian)
Array.Reverse(lengthData);
var length = BitConverter.ToInt32(lengthData, 0);
if (length > 0)
{
var data = new byte[length];
var bytesRead = 0;
while (bytesRead < data.Length)
{
var read = clientSock.Receive(data, bytesRead, data.Length - bytesRead, SocketFlags.None);
if (read <= 0) return;
bytesRead += read;
message += Encoding.UTF8.GetString(data, 0, read);
}
if (message.Contains("CONNECTION") || message.Contains("PONG") || message.Contains("PROC"))
receivedMessage(client, message);
else if (message.Contains("SCREEN"))
receivedImage(data);
}
}
}).Start();
Continue reading...