Java instance code to implement remote desktop
The control side passes the mouse event to the server side
After receiving the mouse event on the server, it transmits it to the client
After the client gets the mouse event, it can be completed through the robot class, and the screenshot will be taken to send the image to the server, and the server will send it to the control terminal.
After I simplify it
//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 Dumb* */public class RobotTest { public static void main(String[] args) throws AWTException { Robot r = new Robot(); r.mouseMove(300, 500);//Mouse move r.mousePress(InputEvent.BUTTON1_MASK ); //Mouse press r.mouseRelease(InputEvent.BUTTON1_MASK);//Mouse release r.keyPress((int)'A'); //Makeboard press (int)'A' means converting A into the corresponding key of the keyboard r.keyRelease((int)'A'); //Makeboard release}}//Screen crawl 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;/** * Crawl local desktop image* @author dumb* */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 xy width height jframe.setVisible(true); jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //Construct a dead loop dynamic intercept while(true){ BufferedImage image = robot.createScreenCapture(new Rectangle(0,0,1366,768)); //Seave the screen label.setIcon(new ImageIcon(image)); Thread.sleep(50); } }} //Explanation of the principle of remote control
//Divid it into server end and client end.
//Originally, the server side is only used as a forwarding, but it does not write forwarding as a demonstration.
// That is, the client side controls the server side E
/**
* What I use here is to send a mouse event on the client side, that is, the control side, after receiving the screen sent by the server side,
* Then use robot to process it
* The transmission method can be processed with socket+io
* Screen capture and image compression adopts robot's screen capture function and JDK's own image encoder to convert it into a byte array
* After sending it to the server, robot can directly get the object object through io+socket, and cast it into InputEvent (both keyEvent and MouseEvent are inherited)
* By judging the event type, you can process it separately. Here, two threads are required on the server, one is to screen and send to the client, and the other is to listen to the client.
* Passed events
*/
//The following is the specific implementation code
//The 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 Dumb* */public class Server { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(80); System.out.println("The server has started normally"); Socket socket = server.accept();//Waiting for receiving the request, blocking the method System.out.println("There is a client connection"); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //Transfer the output streams linked by the client and server side ImageThread processing ImageThread imageThread = new ImageThread(dos); new Thread(imageThread).start(); new Thread(new EventThread(new ObjectInputStream(socket.getInputStream()))).start(); }}/** * Used to handle received mouse events 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();//Import that the object object passed by the client is an object actionEvent(event);//handle 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 judge the event type, and execute it with the robot class* @param event */ public void actionEvent(InputEvent event){ System.out.println(event); if(event instanceof KeyEvent){ KeyEvent e = (KeyEvent)event; int type = e.getID();//Get 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.mouseRelease(getMouseKey(type)); }else if(type == Event.MOUSE_DRAG){ robot.mouseMove(e.getX(), e.getY());//Mouse drag} } } } /** * Return the real event of the mouse. The mouse time cannot be processed directly, and it needs to be converted* @return */ public int getMouseKey(int button){ if(button == MouseEvent.BUTTON1){//Left mouse button return InputEvent.BUTTON1_MASK; }else if(button == MouseEvent.BUTTON2){//Right mouse button return InputEvent.BUTTON2_MASK; }else if(button == MouseEvent.BUTTON3){//Scroller return InputEvent.BUTTON3_MASK; }else{ return 0; } }}//Screen capturer and sender, here you need to get the out stream import of the socket 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;import com.sun.image.codec.jpeg.*;/** * Used to send image data* @author Dumb* */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(); //Seave the entire screen Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); /* int width = (int)dimensional.getWidth(); int height = (int)dimensional.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 Pictures that need to be compressed* @return Compressed byte array* @throws IOException * @throws ImageFormatException */ public byte[] getImageBytes(BufferedImage image) throws ImageFormatException, IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); //Compress the compressor and first get it stored in the byte output stream JPEGImageEncoder jpegd = JPEGCodec.createJPEGEncoder(baos); //Compress iamge jpegd.encode(image); //Convert to byte array return baos.toByteArray(); }}---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 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 Dumb* */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()]; //First get the passed array length dis.readFully(imageBytes); //Storage all data in byte cw.repainImage(imageBytes); } }}/** * Client Form* @author Dumb* */class ClientWindow extends JFrame{ private ObjectOutputStream oos; private JLabel label; //Rewrite 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 scrollbar to the p panel this.add(scroll); this.setSize(1024,768); this.setDefaultCloseOperation(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 above example code for Java implementation of remote desktop is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.