Java Implementation of Remote Desktop instance code, java instance code

Source: Internet
Author: User

Java Implementation of Remote Desktop instance code, java instance code

Java Remote Desktop instance code

The control side transmits the mouse event to the server

The server receives the mouse event and transmits it to the client.

After the client obtains the mouse event, it can use the robot class and send the screenshot to the server. Then, the server sends the screenshot to the Controller.

After being simplified

// First introduce the simple use of the robot class import java. awt. AWTException; import java. awt. robot; import java. awt. event. inputEvent;/*** use robot * @ author dummy $ **/public class RobotTest {public static void main (String [] args) throws AWTException {Robot r = new Robot (); r. mouseMove (300,500); // move the mouse r. mousePress (InputEvent. button#mask); // press r. mouseRelease (InputEvent. button#mask); // release the r. keyPress (int) 'A'); // Press (int) 'A' on the keyboard to convert A to the key r corresponding to the keyboard. keyRelease (int) 'A'); // release the keyboard} // capture the screen import java. awt. AWTException; import java. awt. rectangle; import java. awt. robot; import java. awt. image. bufferedImage; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. windowConstants;/*** capture the local desktop image * @ author dummy $ **/public class ScreenTest {public static void main (String [] args) throws AWTException, interruptedException {Robot robot = new Robot (); JFrame jframe = new JFrame (); jframe. setSize (1200,700); JLabel label = new JLabel (); jframe. add (label); // four parameters x y width height jframe. setVisible (true); jframe. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); // construct an infinite loop to dynamically intercept while (true) {BufferedImage image = robot. createScreenCapture (new Rectangle (0, 0, 1366,768); // capture the screen label. setIcon (new ImageIcon (image); Thread. sleep (50 );}}}

// Remote Control Principle
// Server and client,
// The original server only acts as forwarding. As a demonstration, no forwarding is written.
// The client controls server E.
/**
* Here I use the following method: on the client side, that is, the control side, after receiving the screen sent by the server side, the mouse event is sent.
* Then use robot for processing
* The transmission method can be processed using socket + io.
* Screen capture and Image Compression use the robot screen capture function and the image encoder that comes with jdk to convert it into a byte array.
* After being sent to the server, the robot can directly obtain the object through io + socket and forcibly convert it to InputEvent (both keyEvent and MouseEvent are inherited ).
* You can determine the event type and process them separately. Here, two threads are used on the server. One is to capture and send the screen to the client, and the other is to listen to the client.
* Passed events
*/

// The specific implementation code is as follows:

// Server main process import java. awt. AWTException; import java. awt. event; import java. awt. robot; import java. awt. event. inputEvent; import java. awt. event. keyEvent; import java. awt. event. mouseEvent; import java. io. dataOutputStream; import java. io. IOException; import java. io. objectInputStream; import java.net. serverSocket; import java.net. socket;/*** Server * @ author dummy yuan **/public class Server {public static void main (Stri Ng [] args) throws IOException {ServerSocket server = new ServerSocket (80); System. out. println ("the server has started properly"); Socket socket = server. accept (); // wait for receiving the request. The blocking method is System. out. println ("with client connection"); DataOutputStream dos = new DataOutputStream (socket. getOutputStream (); // submit the output stream linking the client and server to ImageThread to process ImageThread imageThread = new ImageThread (dos); new Thread (imageThread ). start (); new Thread (new EventThread (new ObjectInputStream (socket. getInputStream ()))). start () ;}}/*** is used to handle received mouse or Keyboard Events */class EventThread implements Runnable {private ObjectInputStream ois; private Robot robot; public EventThread (ObjectInputStream ois) {this. ois = ois ;}@ Override public void run () {try {robot = new Robot (); while (true) {InputEvent event = (InputEvent) ois. readObject (); // It is known that the client has passed an object actionEvent ); // Process event} catch (AWTException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {ois. close ();} catch (IOException e) {e. printStackTrace () ;}}/ *** event processing, used to determine the event type, and used the robot class to execute * @ param event */public void actionEvent (InputEvent event) {System. out. println (event); if (event instanceof KeyEvent) {KeyEve Nt e = (KeyEvent) event; int type = e. getID (); // obtain the Event type if (type = Event. KEY_PRESS) {robot. keyPress (e. getKeyCode ();} else if (type = Event. KEY_RELEASE) {robot. keyRelease (e. getKeyCode () ;}} else if (event instanceof MouseEvent) {MouseEvent e = (MouseEvent) event; int type = e. getID (); if (type = Event. MOUSE_MOVE) {robot. mouseMove (e. getX (), e. getY ();} else if (type = Event. MOUSE_DOWN) {robot. mousePress (GetMouseKey (type);} else if (type = Event. MOUSE_UP) {robot. mouserelkey (getMouseKey (type);} else if (type = Event. MOUSE_DRAG) {robot. mouseMove (e. getX (), e. getY (); // drag the mouse}/*** returns the real event of the mouse. The mouse time cannot be processed directly, the conversion * @ return */public int getMouseKey (int button) {if (button = MouseEvent. BUTTON1) {// left mouse button return InputEvent. button#mask;} else if (button = MouseEvent. BUTTON2) {// right-click return InputEvent. BUT TON2_MASK;} else if (button = MouseEvent. BUTTON3) {// return InputEvent. BUTTON3_MASK;} else {return 0 ;}}// the screen interceptor and transmitter. You need to obtain the socket out stream import java. awt. AWTException; import java. awt. dimension; import java. awt. rectangle; import java. awt. robot; import java. awt. toolkit; import java. awt. image. bufferedImage; import java. io. byteArrayOutputStream; import java. io. dataOutputStream; import java. io. IOException; impo Rt com.sun.image.codec.jpeg. *;/*** is used to send image data * @ author dummy RMB **/public class ImageThread implements Runnable {DataOutputStream dos = null; // data output stream public ImageThread (DataOutputStream dos) {this. dos = dos ;}@ Override public void run () {try {Robot robot = new Robot (); // capture the entire screen Dimension = Toolkit. getdefatooltoolkit (). getScreenSize ();/* int width = (int) dimension. getWidth (); int height = (Int) dimension. getWidth (); Rectangle rec = new Rectangle (0, 0, width, height); */Rectangle rec = new Rectangle (dimension); BufferedImage image; byte imageBytes []; while (true) {image = robot. createScreenCapture (rec); imageBytes = getImageBytes (image); dos. writeInt (imageBytes. length); dos. write (imageBytes); dos. flush (); Thread. sleep (50); // thread sleep} catch (AWTException e) {e. printStackTrace ();} catch (ImageFormatException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} catch (InterruptedException e) {e. printStackTrace ();} finally {try {if (dos! = Null) dos. close ();} catch (IOException e) {e. printStackTrace ();}}} /*** compressed image ** @ param image to be compressed * @ return the compressed byte array * @ throws IOException * @ throws ImageFormatException */public byte [] getImageBytes (BufferedImage image) throws ImageFormatException, IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream (); // compress the compressed file and first obtain the required imageencoder required d = required Codec in the byte output stream. createJPEGEncoder (baos); // compress iamge to mongod. encode (image); // convert to byte array return baos. toByteArray () ;}}// client, used to receive creen images and send mouse events import java. awt. event. inputEvent; import java. awt. event. keyEvent; import java. awt. event. keyListener; import java. awt. event. mouseEvent; import java. awt. event. mouseListener; import java. io. dataInputStream; import java. io. IOException; import java. io. objectOutputStream; import java.net. socket; import java.net. unknownHostException; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. windowConstants;/*** Client * @ author dummy **/public class Client {public static void main (String args []) throws UnknownHostException, IOException {Socket s = new Socket ("127.0.0.1", 80); DataInputStream dis = new DataInputStream (s. getInputStream (); ObjectOutputStream oos = new ObjectOutputStream (s. getOutputStream (); ClientWindow cw = new ClientWindow (oos); byte [] imageBytes; while (true) {imageBytes = new byte [dis. readInt ()]; // get the passed array length dis first. readFully (imageBytes); // all data is stored in byte cw. repainImage (imageBytes) ;}}/ *** client form * @ author dummy $ **/class ClientWindow extends JFrame {private ObjectOutputStream oos; private JLabel label; // override the background image method public void repainImage (byte [] imageBytes) {label. setIcon (new ImageIcon (imageBytes); this. repaint ();} public ClientWindow (ObjectOutputStream oos) {this. oos = oos; this. setTitle ("Remote Control Program"); label = new JLabel (); JPanel p = new JPanel (); p. add (label); JScrollPane scroll = new JScrollPane (p); // add a scroll bar to the p panel this. add (scroll); this. setSize (1024,768); this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); this. setVisible (true); this. addKeyListener (new KeyListener () {@ Override public void keyTyped (KeyEvent e) {// TODO Auto-generated method stub} @ Override public void keyReleased (KeyEvent e) {sendEvent (e) ;}@ Override public void keyPressed (KeyEvent e) {sendEvent (e) ;}}); label. addMouseListener (new MouseListener () {@ Override public void mouseReleased (MouseEvent e) {sendEvent (e) ;}@ Override public void mousePressed (MouseEvent e) {sendEvent (e );} @ Override public void mouseClicked (MouseEvent e) {sendEvent (e) ;}@ Override public void mouseEntered (MouseEvent e) {// TODO Auto-generated method stub} @ Override public void mouseExited (MouseEvent e) {// TODO Auto-generated method stub});} public void sendEvent (InputEvent event) {try {oos. writeObject (event);} catch (IOException e) {e. printStackTrace ();}}}

The example code for implementing Remote Desktop in java above is all the content shared by Alibaba Cloud. I hope you can give me a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.