java实现远程桌面监控
来源:互联网 发布:网页美工属于web什么 编辑:程序博客网 时间:2024/05/22 01:47
实现原理其实很简单,在被监视者的机器上,运行一个线程,每隔一段时间就自动截图,并把截图压缩发送到指定的机器上;在监视机器上,也是运行一个线程,接收发送过来的图片包,解压,并绘制到当前的窗口上。这样就基本完成了。
public class Server extends Thread {private Dimension screenSize;private Rectangle rectangle;private Robot robot;public Server() {screenSize = Toolkit.getDefaultToolkit().getScreenSize();rectangle = new Rectangle(screenSize);// 可以指定捕获屏幕区域try {robot = new Robot();} catch (Exception e) {e.printStackTrace();System.out.println(e);}}public void run() {ZipOutputStream os = null;Socket socket = null;while (true) {try {socket = new Socket("192.168.1.100", 5001);// 连接远程IPBufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域os = new ZipOutputStream(socket.getOutputStream());// 加入压缩流// os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));os.setLevel(9);os.putNextEntry(new ZipEntry("test.jpg"));JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEGos.close();Thread.sleep(50);// 每秒20帧} catch (Exception e) {e.printStackTrace();} finally {if (os != null) {try {os.close();} catch (Exception ioe) {}}if (socket != null) {try {socket.close();} catch (IOException e) {}}}}}public static void main(String[] args) {new Server().start();}}
客户端:
public class Client extends JFrame {private static final long serialVersionUID = 1L;Dimension screenSize;public Client() {super();screenSize = Toolkit.getDefaultToolkit().getScreenSize();this.setSize(800, 640);Screen p = new Screen();Container c = this.getContentPane();c.setLayout(new BorderLayout());c.add(p, SwingConstants.CENTER);new Thread(p).start();SwingUtilities.invokeLater(new Runnable(){public void run() {setVisible(true);}});}public static void main(String[] args) {new Client();}class Screen extends JPanel implements Runnable {private static final long serialVersionUID = 1L;private Image cimage;public void run() {ServerSocket ss = null;try {ss = new ServerSocket(5001);// 探听5001端口的连接while (true) {Socket s = null;try {s = ss.accept();ZipInputStream zis = new ZipInputStream(s.getInputStream());zis.getNextEntry();cimage = ImageIO.read(zis);// 把ZIP流转换为图片repaint();} catch (Exception e) {e.printStackTrace();} finally {if (s != null) {try {s.close();} catch (IOException e) {e.printStackTrace();}}}}} catch (Exception e) {} finally {if (ss != null) {try {ss.close();} catch (IOException e) {e.printStackTrace();}}}}public Screen() {super();this.setLayout(null);}public void paint(Graphics g) {super.paint(g);Graphics2D g2 = (Graphics2D) g;g2.drawImage(cimage, 0, 0, null);}}}
转帖:http://flypig.iteye.com/blog/383010
-------------------------------------------------------------------------------------------------------------------------------
1) RemoteClient
Connect to Server
System.out.println("Connecting to server .........."); socket = new Socket(ip, port); System.out.println("Connection Established.");
Capture Desktop Screen then Send it to the Server Periodically
In ScreenSpyer
class, Screen
is captured using createScreenCapture
method inRobot
class 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 implementSerializable
interface. That is why we have to wrap it using the ImageIcon
class as shown below:
while(continueLoop){ //Capture screen BufferedImage image = robot.createScreenCapture(rectangle); /* I have to wrap BufferedImage with ImageIcon because * BufferedImage class does not implement Serializable interface */ ImageIcon imageIcon = new ImageIcon(image); //Send captured screen to the server try { System.out.println("before sending image"); oos.writeObject(imageIcon); oos.reset(); //Clear ObjectOutputStream cache System.out.println("New screenshot sent"); } catch (IOException ex) { ex.printStackTrace(); } //wait for 100ms to reduce network traffic try{ Thread.sleep(100); }catch(InterruptedException e){ e.printStackTrace(); } }
Receive Server Events then call Robot Class Methods to Execute these Events
while(continueLoop){ //receive commands and respond accordingly System.out.println("Waiting for command"); int command = scanner.nextInt(); System.out.println("New command: " + command); switch(command){ case -1: robot.mousePress(scanner.nextInt()); break; case -2: robot.mouseRelease(scanner.nextInt()); break; case -3: robot.keyPress(scanner.nextInt()); break; case -4: robot.keyRelease(scanner.nextInt()); break; case -5: robot.mouseMove(scanner.nextInt(), scanner.nextInt()); break; } }
2) RemoteServer
Wait for Clients Connections
//Listen to server port and accept clients connections while(true){ Socket client = sc.accept(); System.out.println("New client Connected to the server"); //Per each client create a ClientHandler new ClientHandler(client,desktop); }
Receive Client Desktop Screenshots and Display them
while(continueLoop){ //Receive client screenshot and resize it to the current panel size ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject(); System.out.println("New image received"); Image image = imageIcon.getImage(); image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight(),Image.SCALE_FAST); //Draw the received screenshot Graphics graphics = cPanel.getGraphics(); graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);}
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:
public void mouseMoved(MouseEvent e) { double xScale = clientScreenDim.getWidth()/cPanel.getWidth(); System.out.println("xScale: " + xScale); double yScale = clientScreenDim.getHeight()/cPanel.getHeight(); System.out.println("yScale: " + yScale); System.out.println("Mouse Moved"); writer.println(EnumCommands.MOVE_MOUSE.getAbbrev()); writer.println((int)(e.getX() * xScale)); writer.println((int)(e.getY() * yScale)); writer.flush(); } public void mousePressed(MouseEvent e) { System.out.println("Mouse Pressed"); writer.println(EnumCommands.PRESS_MOUSE.getAbbrev()); int button = e.getButton(); int xButton = 16; if (button == 3) { xButton = 4; } writer.println(xButton); writer.flush(); } public void mouseReleased(MouseEvent e) { System.out.println("Mouse Released"); writer.println(EnumCommands.RELEASE_MOUSE.getAbbrev()); int button = e.getButton(); int xButton = 16; if (button == 3) { xButton = 4; } writer.println(xButton); writer.flush(); } public void keyPressed(KeyEvent e) { System.out.println("Key Pressed"); writer.println(EnumCommands.PRESS_KEY.getAbbrev()); writer.println(e.getKeyCode()); writer.flush(); } public void keyReleased(KeyEvent e) { System.out.println("Mouse Released"); writer.println(EnumCommands.RELEASE_KEY.getAbbrev()); writer.println(e.getKeyCode()); writer.flush(); }
转帖:http://www.codeproject.com/KB/IP/RemoteAdminJava.aspx
- java实现远程桌面监控
- Java实现远程桌面连接
- java实现远程桌面
- 监控远程桌面连接
- vc远程桌面监控
- 远程桌面监控系统
- java 实现文件监控
- java实现文件监控
- java实现文件监控
- java实现屏幕监控
- JAVA实现屏幕抓图 远程桌面控制
- Java实现屏幕抓图,控制远程桌面
- 用JAVA实现的远程桌面控制功能
- JAVA实现屏幕抓图 远程桌面控制
- Java实现屏幕抓图,控制远程桌面
- java监控实现方式参考
- 实现Java监听器全局监控
- Java实现文件监控之一
- VC下OpenGL配置
- Android开发之消息处理机制(二)——消息循环
- WebLogic报错Unable to access the selected application. Exception in AppMerge flows' progression
- 创建linux tomcat+mysql
- 《营销管理》-第2章-审视营销管理的基础结构
- java实现远程桌面监控
- 用OutLook发手机短信
- string,char,char*,char a[], sizeof,strlen(),str.length()...
- 1stOpt——世界领先的非线性曲线拟合,综合优化分析计算软件平台
- strlen与sizeof的区别
- 关于Groovy里面元编程(MetaProgramming)的一点总结
- VIM使用系列之一——配置VIM下编程和代码阅读环境
- 特殊符号
- 抽象类和接口的区别,例子经典