F
FLASHCODER
Guest
I have a android code that was made to send screenshot continuously to server.
On server i have a socket special only to receive these screenshots, but my trouble is that only the first screenshot is processed.
Why this is happening?
Here is the server:
Bitmap _buffer;
private class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024 * 10;
public byte[] buffer = new byte[BufferSize];
}
private void StartListening()
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 100);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void AcceptCallback(IAsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), state);
}
private void Read_Image(IAsyncResult ar)
{
StateObject so = (StateObject)ar.AsyncState;
Socket s = so.workSocket;
try
{
int read = s.EndReceive(ar);
if (read > 0)
{
RecvScreenshot(s);
}
Array.Clear(so.buffer, 0, so.buffer.Length);
s.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), so);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void RecvScreenshot(Socket s)
{
var lengthData = new byte[4];
var lengthBytesRead = 0;
do
{
var read = s.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read == 0) return;
lengthBytesRead += read;
} while (lengthBytesRead < lengthData.Length);
Array.Reverse(lengthData);
var length = BitConverter.ToInt32(lengthData, 0);
if (length > 0)
{
var imageData = new byte[length];
var imageBytesRead = 0;
do
{
var read = s.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
if (read == 0) return;
imageBytesRead += read;
} while (imageBytesRead < imageData.Length);
using (var stream = new MemoryStream(imageData))
{
var bitmap = new Bitmap(stream);
Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
}
}
}
private delegate void ImageCompleteDelegate(Bitmap bitmap);
private void ImageComplete(Bitmap bitmap)
{
if (_buffer != null)
{
_buffer.Dispose();
}
_buffer = new Bitmap(bitmap);
pictureBox1.Size = _buffer.Size;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_buffer == null) return;
e.Graphics.DrawImage(_buffer, 0, 0);
}
Here is client (Java Android), normally on android not is necessary send a prefixed string before, but to receive on C# this was necessary (or nothing could be received):
try {
dos = new DataOutputStream(SocketBackgroundService.clientSocket.getOutputStream());
dos.write("SCREEN".getBytes(StandardCharsets.UTF_8));
dos.writeInt(array.length);
dos.write(array, 0, array.length);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
Continue reading...
On server i have a socket special only to receive these screenshots, but my trouble is that only the first screenshot is processed.
Why this is happening?
Here is the server:
Bitmap _buffer;
private class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024 * 10;
public byte[] buffer = new byte[BufferSize];
}
private void StartListening()
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 100);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void AcceptCallback(IAsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), state);
}
private void Read_Image(IAsyncResult ar)
{
StateObject so = (StateObject)ar.AsyncState;
Socket s = so.workSocket;
try
{
int read = s.EndReceive(ar);
if (read > 0)
{
RecvScreenshot(s);
}
Array.Clear(so.buffer, 0, so.buffer.Length);
s.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(Read_Image), so);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void RecvScreenshot(Socket s)
{
var lengthData = new byte[4];
var lengthBytesRead = 0;
do
{
var read = s.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead, SocketFlags.None);
if (read == 0) return;
lengthBytesRead += read;
} while (lengthBytesRead < lengthData.Length);
Array.Reverse(lengthData);
var length = BitConverter.ToInt32(lengthData, 0);
if (length > 0)
{
var imageData = new byte[length];
var imageBytesRead = 0;
do
{
var read = s.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead, SocketFlags.None);
if (read == 0) return;
imageBytesRead += read;
} while (imageBytesRead < imageData.Length);
using (var stream = new MemoryStream(imageData))
{
var bitmap = new Bitmap(stream);
Invoke(new ImageCompleteDelegate(ImageComplete), new object[] { bitmap });
}
}
}
private delegate void ImageCompleteDelegate(Bitmap bitmap);
private void ImageComplete(Bitmap bitmap)
{
if (_buffer != null)
{
_buffer.Dispose();
}
_buffer = new Bitmap(bitmap);
pictureBox1.Size = _buffer.Size;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (_buffer == null) return;
e.Graphics.DrawImage(_buffer, 0, 0);
}
Here is client (Java Android), normally on android not is necessary send a prefixed string before, but to receive on C# this was necessary (or nothing could be received):
try {
dos = new DataOutputStream(SocketBackgroundService.clientSocket.getOutputStream());
dos.write("SCREEN".getBytes(StandardCharsets.UTF_8));
dos.writeInt(array.length);
dos.write(array, 0, array.length);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
Continue reading...