Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code

JVNC Gives You Robotic Control
Apply a Java class to control other computers over the Internet from one machine
by Michael Abernethy and Kulvir Singh Bhogal

Posted July 7, 2004

Although it doesn't offer the advanced artificial intelligence that you'll see in those special-effects-packed movies this summer, Java's java.awt.Robot class nevertheless has many practical and powerful uses that you can add to your applications. Many of you may have read about the Robot class when it came out in JDK 1.3, and said to yourselves, "Wow, that's neat, but what on earth would anyone use it for?" A Google search for "what the heck is java.awt.Robot for?" even turns up many thousands of hits. Obviously, you weren't alone in that thought. Like most new things in Java, once you see a working example it gets the brain cells snapping and you can see visions of it working its way into your current project.

ADVERTISEMENT

Here we will show you how the Robot class functions using the best example we can think of: the popular Virtual Network Computing (VNC) application. For those unfamiliar with VNC, it allows a user to interact with one computer from another computer anywhere on the Internet. In layman's terms, VNC is a client-server application that allows the client to control the server computer by manipulating a screen on the client computer.

The Robot class offers public API's that interact with the native OS to control mouse clicks and keyboard presses. This demonstration will introduce JVNC, a Java application that mimics the popular VNC application. JVNC is a simple, client-server application where the client will be sending mouse actions and keyboard presses to the server, which will use the Robot class to convert the client messages to OS actions. Let's take a look at how JVNC looks when in use (see Figures 1 and 2).

To see exactly how JVNC works, let's first look at the server-side code and see how it gathers messages from clients and converts them into Robot actions.

A Robot Event
The server code sits on the ServerSocket.accept() method waiting for a connection from a client. When a connection is made, a new RobotThread() is spawned, so that the server-side computer can handle multiple clients' messages concurrently:

ServerSocket s = 
  new ServerSocket(1066);
while (true)
{
  Socket socket = s.accept();
  RobotThread t = 
    new RobotThread(socket);
  Thread thread = new Thread(t);
  thread.start();
}

Once the RobotThread() is created, it creates all the resources it will need to both accept the messages from the clients and to respond to the clients with the screen shots of the server machine's screen after a Robot event is performed that triggers a mouse or keyboard event.

The RobotThread() also takes the responsibility for creating the java.awt.Robot object that will be used by the thread throughout its life. In addition, once the Robot object is created, it takes the first of many screen shots of the server machine and sends it back to the client—in effect creating a video stream of the server desktop to the client machines.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home