Click here to Skip to main content
11,412,833 members (76,466 online)
Click here to Skip to main content

Java Remote Desktop Administration

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

Actual Client Desktop

client_screen_small.JPG

Remote Image of the Connected Client Desktop

connected_client_small.JPG

Introduction 

This program allows any computer to control other PCs remotely.   

Program Features

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

Background  

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 RemoteServer_source.zip and RemoteClient_source.zip 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    

RemoteServer

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.

RemoteClient   

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:

while(continueLoop){
            //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.writeObject(imageIcon);
                oos.reset(); //Clear ObjectOutputStream cache
                System.out.println("New screenshot sent");
            } catch (IOException ex) {
               ex.printStackTrace();
            }

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

2) RemoteServer

Wait for Clients Connections 
//Listen to server port and accept clients connections
            while(true){
                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
 while(continueLoop){
 //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
	(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST);
  //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(EnumCommands.MOVE_MOUSE.getAbbrev());
        writer.println((int)(e.getX() * xScale));
        writer.println((int)(e.getY() * yScale));
        writer.flush();
    }

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

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


    public void keyPressed(KeyEvent e) {
        System.out.println("Key Pressed");
        writer.println(EnumCommands.PRESS_KEY.getAbbrev());
        writer.println(e.getKeyCode());
        writer.flush();
    }

    public void keyReleased(KeyEvent e) {
        System.out.println("Mouse Released");
        writer.println(EnumCommands.RELEASE_KEY.getAbbrev());
        writer.println(e.getKeyCode());
        writer.flush();
    }

License

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

Share

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.

Comments and Discussions

 
Questionregarding ip or connection Pin
Member 11534453 at 22-Mar-15 21:45
memberMember 1153445322-Mar-15 21:45 
Questioncan't access another computer Pin
Member 11534453 at 18-Mar-15 4:38
memberMember 1153445318-Mar-15 4:38 
QuestionRegarding the client service. Pin
Member 11450037 at 13-Feb-15 3:14
memberMember 1145003713-Feb-15 3:14 
QuestionRegarding the client service. Pin
Member 11450037 at 13-Feb-15 3:07
memberMember 1145003713-Feb-15 3:07 
Questionvoice nd video conferencing Pin
Member 10456332 at 8-Jan-15 8:17
memberMember 104563328-Jan-15 8:17 
Questionproblem in code Pin
Member 11272638 at 2-Dec-14 23:25
memberMember 112726382-Dec-14 23:25 
Questionthanks Pin
Member 11155812 at 1-Dec-14 8:30
memberMember 111558121-Dec-14 8:30 
GeneralThanks Pin
Member 10277308 at 10-Jul-14 0:16
memberMember 1027730810-Jul-14 0:16 
GeneralRe: sir Pin
Member 11070927 at 9-Sep-14 20:58
memberMember 110709279-Sep-14 20:58 
QuestionNeenah Pin
Member 10809719 at 1-Jun-14 9:22
memberMember 108097191-Jun-14 9:22 
QuestionQuestion Pin
Member 10809719 at 1-Jun-14 9:21
memberMember 108097191-Jun-14 9:21 
QuestionRegarding Screenshot Pin
Member 9461425 at 5-May-14 19:26
memberMember 94614255-May-14 19:26 
AnswerRe: Regarding Screenshot Pin
Member 10809719 at 1-Jun-14 9:25
memberMember 108097191-Jun-14 9:25 
QuestionRegarding screenshot Pin
Member 10465041 at 12-Feb-14 21:48
memberMember 1046504112-Feb-14 21:48 
Questionsome one please please please help..its urgent Pin
Member 10535925 at 28-Jan-14 23:18
memberMember 1053592528-Jan-14 23:18 
AnswerRe: some one please please please help..its urgent Pin
Member 10277308 at 10-Jul-14 0:28
memberMember 1027730810-Jul-14 0:28 
Questionnot getting server screen in client machine Pin
Member 10535925 at 27-Jan-14 1:40
memberMember 1053592527-Jan-14 1:40 
Questionit is " urgent "monitoring more than two computers pleace............. Pin
Member 10492202 at 30-Dec-13 9:27
memberMember 1049220230-Dec-13 9:27 
QuestionUnable to get the client's screen Pin
LLyne Nongsiej at 4-Nov-13 21:14
memberLLyne Nongsiej4-Nov-13 21:14 
BugException encountered ** URGENT** Pin
Member 10346961 at 19-Oct-13 20:34
memberMember 1034696119-Oct-13 20:34 
QuestionxButton Pin
mirkomiredjuric at 4-Sep-13 4:40
membermirkomiredjuric4-Sep-13 4:40 
Questioni want compleat project Pin
krishnpalchouhan at 28-Aug-13 8:38
memberkrishnpalchouhan28-Aug-13 8:38 
QuestionBlank Screen Pin
yuki.pard at 25-Aug-13 19:25
memberyuki.pard25-Aug-13 19:25 
Questioncode will not working the required mission Pin
ahmedabdo2005 at 12-May-13 9:00
memberahmedabdo200512-May-13 9:00 
QuestionProblem in working Pin
Diks20 at 17-Apr-13 8:56
memberDiks2017-Apr-13 8:56 
Questionwrong? Pin
hu_du at 13-Feb-13 9:45
memberhu_du13-Feb-13 9:45 
QuestionHow to make faster transmitting process? Pin
Baubek at 6-Feb-13 22:34
memberBaubek6-Feb-13 22:34 
AnswerRe: How to make faster transmitting process? Pin
Lofi007 at 15-Jun-13 23:09
memberLofi00715-Jun-13 23:09 
Questionplease I Need Help Pin
manjunath ramigani at 24-Jan-13 2:47
membermanjunath ramigani24-Jan-13 2:47 
GeneralThank you so much dear Pin
manjunath ramigani at 22-Jan-13 7:36
membermanjunath ramigani22-Jan-13 7:36 
GeneralMy vote of 4 Pin
coder_prakash at 1-Jan-13 7:20
membercoder_prakash1-Jan-13 7:20 
QuestionNew feature of java remote desktop administration Pin
Arvindbiet at 6-Nov-12 5:56
memberArvindbiet6-Nov-12 5:56 
QuestionWant to view multiple clients in a single server. Pin
Member 9548382 at 26-Oct-12 4:30
memberMember 954838226-Oct-12 4:30 
QuestionImpossible connection! Pin
Member 9470853 at 30-Sep-12 6:19
memberMember 947085330-Sep-12 6:19 
Questionmultiple clients one server Pin
Pr6989 at 27-Jul-12 1:34
memberPr698927-Jul-12 1:34 
QuestionHow to compile? Pin
ArmyyStrongg at 10-Jun-12 0:31
memberArmyyStrongg10-Jun-12 0:31 
Questionconnection timed out error Pin
Pr6989 at 19-Apr-12 23:55
memberPr698919-Apr-12 23:55 
AnswerRe: connection timed out error Pin
Member 8697687 at 29-Apr-12 22:34
memberMember 869768729-Apr-12 22:34 
Questionshould I activate the telnet service to applicate the source code ? Pin
yemen_mansour at 15-Apr-12 9:54
memberyemen_mansour15-Apr-12 9:54 
GeneralMy vote of 5 Pin
dmthuan1 at 11-Apr-12 5:14
memberdmthuan111-Apr-12 5:14 
Questioncontrol using mobile phone Pin
Member 8524924 at 1-Apr-12 4:52
memberMember 85249241-Apr-12 4:52 
QuestionUnable to get/send screenshots over internet .. Pin
r4jiv007 at 16-Mar-12 5:44
memberr4jiv00716-Mar-12 5:44 
GeneralMy vote of 5 Pin
beriaw at 3-Dec-11 3:07
memberberiaw3-Dec-11 3:07 
Questionmake it can run some function Pin
rere rara at 28-Nov-11 4:05
memberrere rara28-Nov-11 4:05 
QuestionThanks man Pin
Ranier Amorim at 11-Nov-11 3:02
memberRanier Amorim11-Nov-11 3:02 
Questionip address Pin
manasvi.karanam at 6-Nov-11 19:41
membermanasvi.karanam6-Nov-11 19:41 
QuestionNeed to Increase Image quality Pin
biomanimca at 26-Jul-11 3:47
memberbiomanimca26-Jul-11 3:47 
QuestionDoc Pin
Member 8066110 at 7-Jul-11 4:31
memberMember 80661107-Jul-11 4:31 
Generaldoc Pin
naveen1989 at 9-Apr-11 18:41
membernaveen19899-Apr-11 18:41 
GeneralHi Lemo... Pin
aniket mahajan at 27-Feb-11 18:45
memberaniket mahajan27-Feb-11 18:45 
Hi..
I am a comp science student n I want to do a similar application on android..

Please reply if you hav any idea abt hw to develop such app..

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

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

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