How can I make this to Multi-Client Server Remote Desktop Viewer

  • Thread starter Thread starter BryanBalaga
  • Start date Start date
B

BryanBalaga

Guest
Hi. I am a student and new with c# programming and I wanted to create a multi-client that has a remote desktop viewer functionality.

I have a code here but only accepts one client.

Server.cs

private TcpClient client;
private TcpListener server;
private NetworkStream mainStream;

private readonly Thread Listening;
private readonly Thread GetImage;

public frmRemoteView()
{
try
{
this.client = new TcpClient();
this.Listening = new Thread(StartListening);
this.GetImage = new Thread(ReceiveImage);
InitializeComponent();
}
catch (Exception ex)
{
MessageBox.Show("Error 1: " + ex.Message);
}
}

private void StartListening()
{
try
{
while (!this.client.Connected)
{
this.server.Start();
this.client = server.AcceptTcpClient();
}
this.GetImage.Start();
}
catch (Exception ex)
{
MessageBox.Show("Error 2: " + ex.Message);
}
}

private void StopListening()
{
this.server.Stop();
this.client = null;
if (this.Listening.IsAlive) this.Listening.Abort();
if (this.GetImage.IsAlive) this.GetImage.Abort();
}

private void ReceiveImage()
{
BinaryFormatter binFormatter = new BinaryFormatter();
while (this.client.Connected)
{
try
{
this.mainStream = this.client.GetStream();
pictureBoxRemoteView.Image =
(Image)binFormatter.Deserialize(this.mainStream);

if (!client.Connected)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error 3: " + ex.Message);
}

}
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.server = new TcpListener(IPAddress.Any, 8888);
this.Listening.Start();
}



Client.cs

private readonly TcpClient client = new TcpClient();
private NetworkStream mainStream;

public Form1()
{
InitializeComponent();
}

private static Image GrabDesktop()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
Graphics graphic = Graphics.FromImage(screenshot);
graphic.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}

private void SendDesktopImage()
{
try
{
BinaryFormatter binFormatter = new BinaryFormatter();
mainStream = client.GetStream();
binFormatter.Serialize(mainStream,
GrabDesktop());
}
catch (Exception ex)
{
timer1.Stop();
MessageBox.Show(ex.Message);
}
}

private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}

private void Form1_Load(object sender, EventArgs e)
{
client.Connect("127.0.0.1", 8888);
timer1.Start();
}

I also used the logic behind this project to accept multiple-client but failed.

codeproject.com/Articles/16023/Multithreaded-Chat-Server

Please help me.

Thank you and Best Regards!

Continue reading...
 
Back
Top