Click here to Skip to main content
13,356,787 members (60,711 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 1 May 2009

Java Remote Desktop Administration

, 2 May 2009
Rate this:
Please Sign up or sign in to vote.
Control And View Another Computer Remotely

Actual Client Desktop


Remote Image of the Connected Client Desktop



This program allows any computer to control other PCs remotely.   

Program Features

  • View remote desktop
  • Mouse movement control
  • Mouse buttons control
  • Keyboard control


One of the biggest challenges in this project is finding a way to move the mouse pointer, simulate key stroke and capture the screen. After spending some time I found a great class, Robot class that does all that I need.

In addition, using serialization to send screenshots from client to server and to send server events like mouse move, key press, key release  helped me a lot to write clean and simple code instead of sending images and events in raw data format over the network. 

Robot Class Methods 

  • mouseMove - Moves the mouse pointer to a set of specified absolute screen coordinates given in pixels
  • mousePress - Presses one of the buttons on the mouse
  • mouseRelease - Releases one of the buttons on the mouse
  • keyPress - Presses a specified key on the keyboard
  • keyRelease - Releases specified key on the keyboard
  • createScreenCapture - Takes a screenshot  

Program Parts    

1. RemoteServer 

This is the server part which waits for clients connections and per each connected client, a new frame appears showing the current client screen. When you move the mouse over the frame, this results in moving the mouse at the client side. The same happens when you right/left click mouse button or type a key while the frame is in focus.

2. RemoteClient  

This the client side, its core function is sending a screen shot of the client's desktop every predefined amount of time. Also it receives server commands such as "move the mouse command", then executes the command at the client's PC.  

Using the Code 

In order to run the program, you need to download and on two different PCs, then extract them. In one of the PCs say PC1, run RemoteServer.jar which is located under dist folder using the following command: 

>> java -jar  RemoteServer.jar 

You will be asked to enter port number for the server to listen at, enter any port number above 1024, for example 5000.   

On the other PC say PC2, execute RemoteClient.jar using the following command:  

>> java -jar RemoteClient.jar  

You will be asked to enter server IP, enter IP address of PC1, then you will be asked to enter port number, enter the same port you entered above, e.g. 5000. 

Now, in PC1 you have full control over PC2 including moving the mouse, clicking the mouse, keys stroking, viewing PC2 desktop, etc.  

Coding Structure    


ServerInitiator  Class

This is the entry class which listens to server port and wait for clients connections. Also, it creates an essential part of the program GUI.

ClientHandler Class 

Per each connected client, there is an object of this class. It shows an InternalFrame per client and it receives clients' screen dimension.

ClientScreenReciever Class 

Receives captured screen from the client, then displays it.

ClientCommandsSender Class

It listens to the server commands, then sends them to the client. Server commands include mouse move, key stroke, mouse click, etc.

EnumCommands Class

Defines constants which are used to represent server commands.


ClientInitiator Class 

This is the entry class that starts the client instance. It establishes connection to the server and creates the client GUI.

ScreenSpyer Class  

Captures screen periodically and sends them to the server.

ServerDelegate Class   

Receives server commands and executes them in the client PC.

EnumCommands Class  

Defines constants which are used to represent server commands.   

Code Snippets

1) RemoteClient  

Connect to Server 
 System.out.println("Connecting to server ..........");
 socket = new Socket(ip, port);
 System.out.println("Connection Established.");  
Capture Desktop Screen then Send it to the Server Periodically  

In ScreenSpyer class, Screen is captured using  createScreenCapture method in Robot class and it accepts a Rectangle object which carries screen dimension. If we try to send image object directly using serialization, it will fail because it does not implement Serializable interface. That is why we have to wrap it using the ImageIcon class as shown below:

            //Capture screen
            BufferedImage image = robot.createScreenCapture(rectangle);
            /* I have to wrap BufferedImage with ImageIcon because 
	     * BufferedImage class does not implement Serializable interface
            ImageIcon imageIcon = new ImageIcon(image);

            //Send captured screen to the server
            try {
                System.out.println("before sending image");
                oos.reset(); //Clear ObjectOutputStream cache
                System.out.println("New screenshot sent");
            } catch (IOException ex) {

            //wait for 100ms to reduce network traffic
            }catch(InterruptedException e){
Receive Server Events then call Robot Class Methods to Execute these Events 
                //receive commands and respond accordingly
                System.out.println("Waiting for command");
                int command = scanner.nextInt();
                System.out.println("New command: " + command);
                    case -1:
                    case -2:
                    case -3:
                    case -4:
                    case -5:
                        robot.mouseMove(scanner.nextInt(), scanner.nextInt());

2) RemoteServer

Wait for Clients Connections 
//Listen to server port and accept clients connections
                Socket client = sc.accept();
                System.out.println("New client Connected to the server");
                //Per each client create a ClientHandler
                new ClientHandler(client,desktop);
Receive Client Desktop Screenshots and Display them
 //Receive client screenshot and resize it to the current panel size
  ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();
  System.out.println("New image received");
  Image image = imageIcon.getImage();
  image = image.getScaledInstance
  //Draw the received screenshot
  Graphics graphics = cPanel.getGraphics();
  graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
Handle Mouse and Key Events then Send them to the Client Program to Simulate them 

In ClientCommandsSender class, when mouse is moved, x and y values are sent to the client but we have to take into consideration the size difference between clients' screen size and  server's panel size, that is why we have to multiply by a certain factor as shown in the following code: 

public void mouseMoved(MouseEvent e) {
     double xScale = clientScreenDim.getWidth()/cPanel.getWidth();
     System.out.println("xScale: " + xScale);
     double yScale = clientScreenDim.getHeight()/cPanel.getHeight();
     System.out.println("yScale: " + yScale);
     System.out.println("Mouse Moved");
     writer.println((int)(e.getX() * xScale));
     writer.println((int)(e.getY() * yScale));

 public void mousePressed(MouseEvent e) {
     System.out.println("Mouse Pressed");
     int button = e.getButton();
     int xButton = 16;
     if (button == 3) {
         xButton = 4;

 public void mouseReleased(MouseEvent e) {
     System.out.println("Mouse Released");
     int button = e.getButton();
     int xButton = 16;
     if (button == 3) {
         xButton = 4;

 public void keyPressed(KeyEvent e) {
     System.out.println("Key Pressed");

 public void keyReleased(KeyEvent e) {
     System.out.println("Mouse Released");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lemo Stein
Software Developer
Egypt Egypt
I have over 5 years of experience in the field of Sofware development and management.
My programming experience includes Java, ASP.Net, PHP, C# C, C++

Recently I'm working as a teaching assistant and team leader for PHP based projects.

You may also be interested in...

Comments and Discussions

QuestionNeed to Increase Image quality Pin
biomanimca26-Jul-11 3:47
memberbiomanimca26-Jul-11 3:47 
QuestionDoc Pin
Member 80661107-Jul-11 4:31
memberMember 80661107-Jul-11 4:31 
Generaldoc Pin
naveen19899-Apr-11 18:41
membernaveen19899-Apr-11 18:41 
GeneralHi Lemo... Pin
aniket mahajan27-Feb-11 18:45
memberaniket mahajan27-Feb-11 18:45 
Questionwhy don't uses UDP protocol Pin
lengocphi4-Nov-10 1:53
memberlengocphi4-Nov-10 1:53 
GeneralA Guide to your codes Pin
franksenior1-Sep-10 4:55
memberfranksenior1-Sep-10 4:55 
QuestionGuidance Pin
rba999914-Jul-10 11:41
memberrba999914-Jul-10 11:41 
QuestionMake it work over Internet for PC's behind proxies Pin
NRworld5-Jul-10 4:38
memberNRworld5-Jul-10 4:38 

What would be the correct approach to modify this application to make it work over internet for remote PC's which are behind firewall and proxies.
I understand that a hosted server will be required. I would like to understand how should I pass the objects between these 3 machines for this app to work.

Please share your views.

GeneralRemote Administration Pin
Drs201010-Mar-10 0:34
memberDrs201010-Mar-10 0:34 
QuestionOptimization Pin
Lemo Stein1-May-09 14:37
memberLemo Stein1-May-09 14:37 
AnswerRe: Optimization Pin
asdfasdfa431-May-09 23:27
memberasdfasdfa431-May-09 23:27 
GeneralRe: Optimization Pin
Lemo Stein2-May-09 0:01
memberLemo Stein2-May-09 0:01 
GeneralRe: Optimization Pin
ashishqwerty1-Sep-09 2:11
memberashishqwerty1-Sep-09 2:11 
GeneralRe: Optimization Pin
anaghashevatikar28-Feb-11 17:32
memberanaghashevatikar28-Feb-11 17:32 
GeneralRe: Optimization Pin
ashishqwerty1-Mar-11 2:41
memberashishqwerty1-Mar-11 2:41 
AnswerRe: Optimization Pin
malaa9913-Feb-10 15:35
membermalaa9913-Feb-10 15:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 2 May 2009
Article Copyright 2009 by Lemo Stein
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid