Making a remote control aplication

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
for a mates business, Its probly not the best way of doing it but Im learning nonetheless.
I started with a sample I found that captures the screen to png and sends the bitmap thru a socket and displays it in a picturebox and that works wonderfully but now I need to be able to send mouse control and key presses I have code that triggers left mouse
down/up events and I can move the mouse cursor with code but I need to figure out how to do the rest like right click and key presses and maybe even scaling the picturebox in case the clients screen is too big.
The main thing is the clients end to be controlled needs to establish the connections to avoid NATs.
Heres the client application, look for the comments thats where work needs to be done:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Net;
<span style="color:Blue; using System.Net.Sockets;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using System.Threading;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; namespace WindowsFormsApplication1
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; bool _go;

<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void Button1Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (_go == <span style="color:Blue; false)
{
ThreadPool.QueueUserWorkItem(Work);
ThreadPool.QueueUserWorkItem(Movemouse);
_go = <span style="color:Blue; true;
}
}

<span style="color:Blue; private <span style="color:Blue; void Movemouse(Object state) <span style="color:Green; //THIS is what im working on now
{
<span style="color:Blue; using (<span style="color:Blue; var socket = <span style="color:Blue; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(<span style="color:Blue; new IPEndPoint(IPAddress.Parse(<span style="color:#A31515; "10.1.1.1"), 48625));
socket.ReceiveBufferSize = 1024;
<span style="color:Blue; while (_go)
{
<span style="color:Blue; var ok = <span style="color:Blue; new <span style="color:Blue; byte[] { 0 };
<span style="color:Blue; var x = <span style="color:Blue; new <span style="color:Blue; byte[32];
<span style="color:Blue; var y = <span style="color:Blue; new <span style="color:Blue; byte[32];
socket.Receive(x);
socket.Receive(y);
<span style="color:Blue; var xy = <span style="color:Blue; new Point(BitConverter.ToInt32(x, 0), BitConverter.ToInt32(y, 0));
Invoke(<span style="color:Blue; new SetXyDelegate(SetXy), <span style="color:Blue; new <span style="color:Blue; object[] { xy });
socket.Send(ok);
}
}
}
<span style="color:Blue; private <span style="color:Blue; const UInt32 MouseeventfLeftdown = 0x0002;
<span style="color:Blue; private <span style="color:Blue; const UInt32 MouseeventfLeftup = 0x0004;
[DllImport(<span style="color:#A31515; "user32.dll")]
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; void mouse_event(<span style="color:Blue; uint dwFlags, <span style="color:Blue; uint dx, <span style="color:Blue; uint dy, <span style="color:Blue; uint dwData, <span style="color:Blue; uint dwExtraInf);
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void SetXy(Point xy)
{
Cursor.Position = xy;
mouse_event(MouseeventfLeftdown, 0, 0, 0, 0);
mouse_event(MouseeventfLeftup, 0, 0, 0, 0);
}

<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void Work(Object state)
{
<span style="color:Blue; using (<span style="color:Blue; var socket = <span style="color:Blue; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(<span style="color:Blue; new IPEndPoint(IPAddress.Parse(<span style="color:#A31515; "10.1.1.1"), 48624));
socket.SendBufferSize = 1048576 * 10;
<span style="color:Blue; while (_go)
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
<span style="color:Blue; var bitmap = <span style="color:Blue; new Bitmap(bounds.Width, bounds.Height);
<span style="color:Blue; try
{
<span style="color:Blue; while (_go)
{
<span style="color:Blue; using (<span style="color:Blue; var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(bounds.X, 0, bounds.Y, 0, bounds.Size);
}
<span style="color:Blue; byte[] imageData;
<span style="color:Blue; using (<span style="color:Blue; var stream = <span style="color:Blue; new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
imageData = stream.ToArray();
}
<span style="color:Blue; byte[] lengthData = BitConverter.GetBytes(imageData.Length);
<span style="color:Blue; if (socket.Send(lengthData) < lengthData.Length) <span style="color:Blue; break;
<span style="color:Blue; if (socket.Send(imageData) < imageData.Length) <span style="color:Blue; break;
}
}
<span style="color:Blue; catch
{
<span style="color:Blue; break;
}
}
}
}

<span style="color:Blue; private <span style="color:Blue; void Button2Click(<span style="color:Blue; object sender, EventArgs e)
{
_go = <span style="color:Blue; false;
}

<span style="color:Blue; #region Nested type: SetXyDelegate

<span style="color:Blue; private <span style="color:Blue; delegate <span style="color:Blue; void SetXyDelegate(Point xy);

<span style="color:Blue; #endregion
}
}
[/code]

<br/>
and heres the server application to control the client from:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Net;
<span style="color:Blue; using System.Net.Sockets;
<span style="color:Blue; using System.Threading;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; namespace WindowsFormsApplication2
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; private Bitmap _bitbuf;
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; bool _go;
<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void Button1Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Blue; if (_go == <span style="color:Blue; false)
{
_go = <span style="color:Blue; true;
button1.Enabled = <span style="color:Blue; false;
ThreadPool.QueueUserWorkItem(GetSnapshots);
ThreadPool.QueueUserWorkItem(Mouse);
}
}

<span style="color:Blue; private <span style="color:Blue; void GetSnapshots(<span style="color:Blue; object state)
{
<span style="color:Blue; using (<span style="color:Blue; var socket = <span style="color:Blue; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(<span style="color:Blue; new IPEndPoint(IPAddress.Any, 48624));
socket.Listen(100);
<span style="color:Blue; using (<span style="color:Blue; var client = socket.Accept())
{
client.ReceiveBufferSize = 1048576*10;
<span style="color:Blue; while (_go)
{
<span style="color:Blue; var lengthData = <span style="color:Blue; new <span style="color:Blue; byte[4];
<span style="color:Blue; int lengthBytesRead = 0;
<span style="color:Blue; while (lengthBytesRead < lengthData.Length)
{
<span style="color:Blue; int read = client.Receive(lengthData, lengthBytesRead, lengthData.Length - lengthBytesRead,
SocketFlags.None);
<span style="color:Blue; if (read == 0) <span style="color:Blue; return;
lengthBytesRead += read;
}
<span style="color:Blue; int length = BitConverter.ToInt32(lengthData, 0);
<span style="color:Blue; var imageData = <span style="color:Blue; new <span style="color:Blue; byte[length];
<span style="color:Blue; int imageBytesRead = 0;
<span style="color:Blue; while (imageBytesRead < imageData.Length)
{
<span style="color:Blue; int read = client.Receive(imageData, imageBytesRead, imageData.Length - imageBytesRead,
SocketFlags.None);
<span style="color:Blue; if (read == 0) <span style="color:Blue; return;
imageBytesRead += read;
}
<span style="color:Blue; using (<span style="color:Blue; var stream = <span style="color:Blue; new MemoryStream(imageData))
{
<span style="color:Blue; var bitmap = <span style="color:Blue; new Bitmap(stream);
Invoke(<span style="color:Blue; new ImageCompleteDelegate(ImageComplete), <span style="color:Blue; new <span style="color:Blue; object[] {bitmap});
}
}
}
}
}

<span style="color:Blue; private <span style="color:Blue; void Mouse(<span style="color:Blue; object state) <span style="color:Green; //This is obviously not going to work
{
<span style="color:Blue; using (<span style="color:Blue; var socket = <span style="color:Blue; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(<span style="color:Blue; new IPEndPoint(IPAddress.Any, 48625));
socket.Listen(100);
<span style="color:Blue; using (<span style="color:Blue; var client = socket.Accept())
{
client.ReceiveBufferSize = 1024;
<span style="color:Blue; while (<span style="color:Blue; true)
{
<span style="color:Blue; var ok = <span style="color:Blue; new <span style="color:Blue; byte[1];
<span style="color:Blue; var x = BitConverter.GetBytes(Cursor.Position.X - pictureBox1.DisplayRectangle.X);
<span style="color:Blue; var y = BitConverter.GetBytes(Cursor.Position.Y - pictureBox1.DisplayRectangle.Y);
client.Send(x);
client.Send(y);
client.Receive(ok);
}
}
}
}
<span style="color:Blue; private <span style="color:Blue; void PictureBox1MouseClick(<span style="color:Blue; object sender, MouseEventArgs e)
{
<span style="color:Green; //What to put here?
}

<span style="color:Blue; private <span style="color:Blue; void ImageComplete(Bitmap bitmap)
{
<span style="color:Blue; if (_bitbuf != <span style="color:Blue; null)
{
_bitbuf.Dispose();
}
_bitbuf = <span style="color:Blue; new Bitmap(bitmap);
pictureBox1.Size = _bitbuf.Size;
pictureBox1.Invalidate();
}

<span style="color:Blue; private <span style="color:Blue; void PictureBox1Paint(<span style="color:Blue; object sender, PaintEventArgs e)
{
<span style="color:Blue; if (_bitbuf == <span style="color:Blue; null) <span style="color:Blue; return;
e.Graphics.DrawImage(_bitbuf, 0, 0);
}

<span style="color:Blue; #region Nested type: ImageCompleteDelegate

<span style="color:Blue; private <span style="color:Blue; delegate <span style="color:Blue; void ImageCompleteDelegate(Bitmap bitmap);

<span style="color:Blue; #endregion

<span style="color:Blue; private <span style="color:Blue; void Button2Click(<span style="color:Blue; object sender, EventArgs e)
{
_go = <span style="color:Blue; false;
button1.Enabled = <span style="color:Blue; true;
}

<span style="color:Blue; private <span style="color:Blue; void Form1Close(<span style="color:Blue; object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}

}
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top