打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java实现远程桌面监控
java里面的Robot类可以完成截图的功能,借助于这点,我尝试着做了一个简陋的桌面监控程序,运行了下,感觉速度还可以,还有很大的优化空间的,比如用udp协议取代tcp等。代码也写的不是很优雅,只在娱乐了。

实现原理其实很简单,在被监视者的机器上,运行一个线程,每隔一段时间就自动截图,并把截图压缩发送到指定的机器上;在监视机器上,也是运行一个线程,接收发送过来的图片包,解压,并绘制到当前的窗口上。这样就基本完成了。

  1. public class Server extends Thread {
  2. private Dimension screenSize;
  3. private Rectangle rectangle;
  4. private Robot robot;
  5. public Server() {
  6. screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  7. rectangle = new Rectangle(screenSize);// 可以指定捕获屏幕区域
  8. try {
  9. robot = new Robot();
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. System.out.println(e);
  13. }
  14. }
  15. public void run() {
  16. ZipOutputStream os = null;
  17. Socket socket = null;
  18. while (true) {
  19. try {
  20. socket = new Socket("192.168.1.100", 5001);// 连接远程IP
  21. BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域
  22. os = new ZipOutputStream(socket.getOutputStream());// 加入压缩流
  23. // os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));
  24. os.setLevel(9);
  25. os.putNextEntry(new ZipEntry("test.jpg"));
  26. JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEG
  27. os.close();
  28. Thread.sleep(50);// 每秒20帧
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. if (os != null) {
  33. try {
  34. os.close();
  35. } catch (Exception ioe) {
  36. }
  37. }
  38. if (socket != null) {
  39. try {
  40. socket.close();
  41. } catch (IOException e) {
  42. }
  43. }
  44. }
  45. }
  46. }
  47. public static void main(String[] args) {
  48. new Server().start();
  49. }
  50. }

客户端:

  1. public class Client extends JFrame {
  2. private static final long serialVersionUID = 1L;
  3. Dimension screenSize;
  4. public Client() {
  5. super();
  6. screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  7. this.setSize(800, 640);
  8. Screen p = new Screen();
  9. Container c = this.getContentPane();
  10. c.setLayout(new BorderLayout());
  11. c.add(p, SwingConstants.CENTER);
  12. new Thread(p).start();
  13. SwingUtilities.invokeLater(new Runnable(){
  14. public void run() {
  15. setVisible(true);
  16. }});
  17. }
  18. public static void main(String[] args) {
  19. new Client();
  20. }
  21. class Screen extends JPanel implements Runnable {
  22. private static final long serialVersionUID = 1L;
  23. private Image cimage;
  24. public void run() {
  25. ServerSocket ss = null;
  26. try {
  27. ss = new ServerSocket(5001);// 探听5001端口的连接
  28. while (true) {
  29. Socket s = null;
  30. try {
  31. s = ss.accept();
  32. ZipInputStream zis = new ZipInputStream(s
  33. .getInputStream());
  34. zis.getNextEntry();
  35. cimage = ImageIO.read(zis);// 把ZIP流转换为图片
  36. repaint();
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. } finally {
  40. if (s != null) {
  41. try {
  42. s.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }
  49. } catch (Exception e) {
  50. } finally {
  51. if (ss != null) {
  52. try {
  53. ss.close();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. public Screen() {
  61. super();
  62. this.setLayout(null);
  63. }
  64. public void paint(Graphics g) {
  65. super.paint(g);
  66. Graphics2D g2 = (Graphics2D) g;
  67. g2.drawImage(cimage, 0, 0, null);
  68. }
  69. }
  70. }

转帖:http://flypig.iteye.com/blog/383010

-------------------------------------------------------------------------------------------------------------------------------

1) RemoteClient

Connect to Server
  1. System.out.println("Connecting to server ..........");
  2. socket = new Socket(ip, port);
  3. System.out.println("Connection Established.");
Capture Desktop Screen then Send it to the Server Periodically

In ScreenSpyer class, Screen is captured using createScreenCapture method inRobotclass 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 implementSerializableinterface. That is why we have to wrap it using the ImageIconclass as shown below:

  1. while(continueLoop){
  2. //Capture screen
  3. BufferedImage image = robot.createScreenCapture(rectangle);
  4. /* I have to wrap BufferedImage with ImageIcon because
  5. * BufferedImage class does not implement Serializable interface
  6. */
  7. ImageIcon imageIcon = new ImageIcon(image);
  8. //Send captured screen to the server
  9. try {
  10. System.out.println("before sending image");
  11. oos.writeObject(imageIcon);
  12. oos.reset(); //Clear ObjectOutputStream cache
  13. System.out.println("New screenshot sent");
  14. } catch (IOException ex) {
  15. ex.printStackTrace();
  16. }
  17. //wait for 100ms to reduce network traffic
  18. try{
  19. Thread.sleep(100);
  20. }catch(InterruptedException e){
  21. e.printStackTrace();
  22. }
  23. }


Receive Server Events then call Robot Class Methods to Execute these Events

  1. while(continueLoop){
  2. //receive commands and respond accordingly
  3. System.out.println("Waiting for command");
  4. int command = scanner.nextInt();
  5. System.out.println("New command: " + command);
  6. switch(command){
  7. case -1:
  8. robot.mousePress(scanner.nextInt());
  9. break;
  10. case -2:
  11. robot.mouseRelease(scanner.nextInt());
  12. break;
  13. case -3:
  14. robot.keyPress(scanner.nextInt());
  15. break;
  16. case -4:
  17. robot.keyRelease(scanner.nextInt());
  18. break;
  19. case -5:
  20. robot.mouseMove(scanner.nextInt(), scanner.nextInt());
  21. break;
  22. }
  23. }


2) RemoteServer

Wait for Clients Connections
  1. //Listen to server port and accept clients connections
  2. while(true){
  3. Socket client = sc.accept();
  4. System.out.println("New client Connected to the server");
  5. //Per each client create a ClientHandler
  6. new ClientHandler(client,desktop);
  7. }


Receive Client Desktop Screenshots and Display them
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:

  1. public void mouseMoved(MouseEvent e) {
  2. double xScale = clientScreenDim.getWidth()/cPanel.getWidth();
  3. System.out.println("xScale: " + xScale);
  4. double yScale = clientScreenDim.getHeight()/cPanel.getHeight();
  5. System.out.println("yScale: " + yScale);
  6. System.out.println("Mouse Moved");
  7. writer.println(EnumCommands.MOVE_MOUSE.getAbbrev());
  8. writer.println((int)(e.getX() * xScale));
  9. writer.println((int)(e.getY() * yScale));
  10. writer.flush();
  11. }
  12. public void mousePressed(MouseEvent e) {
  13. System.out.println("Mouse Pressed");
  14. writer.println(EnumCommands.PRESS_MOUSE.getAbbrev());
  15. int button = e.getButton();
  16. int xButton = 16;
  17. if (button == 3) {
  18. xButton = 4;
  19. }
  20. writer.println(xButton);
  21. writer.flush();
  22. }
  23. public void mouseReleased(MouseEvent e) {
  24. System.out.println("Mouse Released");
  25. writer.println(EnumCommands.RELEASE_MOUSE.getAbbrev());
  26. int button = e.getButton();
  27. int xButton = 16;
  28. if (button == 3) {
  29. xButton = 4;
  30. }
  31. writer.println(xButton);
  32. writer.flush();
  33. }
  34. public void keyPressed(KeyEvent e) {
  35. System.out.println("Key Pressed");
  36. writer.println(EnumCommands.PRESS_KEY.getAbbrev());
  37. writer.println(e.getKeyCode());
  38. writer.flush();
  39. }
  40. public void keyReleased(KeyEvent e) {
  41. System.out.println("Mouse Released");
  42. writer.println(EnumCommands.RELEASE_KEY.getAbbrev());
  43. writer.println(e.getKeyCode());
  44. writer.flush();
  45. }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
多重if
java经典题目练习
Java语言程序设计基础篇第三章编程练习题
Java程序设计总复习题
Java实现方法判断一个数是不是素数(三种不同的方法,都有运行结果展示)
JAVA经典算法50题(4)【面试+工作】
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服