Saturday, May 14, 2011

A Java VNC Server implementation

I found this code today and I really have to test it.

Sources are here : http://ramshacking.blogspot.com/2009/07/server-class-for-vnc.html



import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.event.*;

public class JVNCserver{

private final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
private final Rectangle screenRect = new Rectangle(0, 0, WIDTH, HEIGHT);
public Socket socket;
public ServerSocket s;
private CaptureThread capturethread;
private CaptureEvents captureevents;
private Robot robot;
private ObjectOutputStream out;
private BufferedReader in;
private BufferedImage i;
Image image;

public static void main(String arg[])
{
JVNCserver s = new JVNCserver();
}

public JVNCserver()
{
try
{
s = new ServerSocket(1166);
socket = s.accept();
System.out.println("Server Started");
out = new ObjectOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
robot = new Robot();
capturethread = new CaptureThread();
captureevents = new CaptureEvents();
}

catch(Exception e)
{
JOptionPane.showMessageDialog(null,"server is stop");
}
}

public void sendImage() throws IOException
{
i = robot.createScreenCapture(screenRect);
image = i.getScaledInstance(WIDTH, HEIGHT-60, Image.SCALE_SMOOTH); out.writeObject(new ImageIcon(image));
i.flush();
image.flush();
out.flush();
}

private class CaptureThread implements Runnable
{
private volatile boolean keepRunning;
Thread thread;
CaptureThread()
{
thread = new Thread(this,"Capture Thread");
keepRunning = true;
thread.start();
}
public void run()
{
while (keepRunning)
{
try
{
sendImage();
Thread.currentThread().sleep(8000);
//Thread.currentThread().sleep(20000);
}
catch(InterruptedException e1)
{ System.out.println("Thread Stop"); }
catch (IOException ex) { ex.printStackTrace(); }
catch(Exception e) {
JOptionPane.showMessageDialog(null,"server is stoped"); } } }
public void stopRunning() { keepRunning = false; } }

private class CaptureEvents implements Runnable {
private volatile boolean keepRunning;
Thread thread;
private int HY = HEIGHT / 2;
int y;
CaptureEvents()
{
thread = new Thread(this,"Capture Events");
keepRunning = true;
thread.start();
}
public void run()
{
while (keepRunning)
{
try
{
if (in!=null)
{
String e = (String)in.readLine();
if (e!=null)
{ //System.out.println(e);
int eventType = Integer.parseInt(e.substring(0, e.indexOf("")));
int arg1 = Integer.parseInt(e.substring(e.indexOf("")+1, e.lastIndexOf("")));
int arg2 = Integer.parseInt(e.substring(e.lastIndexOf("")+1));
//System.out.println(arg1+"-"+arg2);
if(eventType==JVNCconstant.CLOSE) { keepRunning = false; in.close(); out.close(); return; }
if(arg2 < y =" arg2"> HY) y = arg2 + 21;
if (eventType == JVNCconstant.MOUSE_MOVE) robot.mouseMove(arg1,y);
else if (eventType == JVNCconstant.MOUSE_PRESS)
{ robot.mousePress(InputEvent.BUTTON1_MASK); }
else if (eventType==JVNCconstant.MOUSE_RELEASE) robot.mouseRelease(InputEvent.BUTTON1_MASK);
else if(eventType== JVNCconstant.MOUSE_WHEEL) robot.mouseWheel(arg1);
else if(eventType == JVNCconstant.KEY_PRESS_EVENTS)
{
switch(arg1)
{
case KeyEvent.VK_ENTER: robot.keyPress(KeyEvent.VK_ENTER); break;
case KeyEvent.VK_F1: robot.keyPress(KeyEvent.VK_F1); break;
case KeyEvent.VK_ESCAPE: robot.keyPress(KeyEvent.VK_ESCAPE); break; } } } }
else
System.out.println("In empty"); //Thread.currentThread().sleep(50); } /*catch(InterruptedException e) { System.out.println("Thread Stop"); }*/ catch(SocketException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(null,"Client is stopped"); break; } catch (IOException ex) { ex.printStackTrace(); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Server is stop"); } } } public void stopRunning() { keepRunning = false; } }
}

1 comment:

Rahul said...

Thanks for the great information in your blog Selenium Training in Chennai

Download Oracle JDK from command line

Found on the web. You can adapt the url with one grabbed from Oracle JDK download page. What I love here is the fact is send Oracle licence ...