监听调试web service的好工具TCPMon

一般的远程监控软件都是用c或者c++等语言开发的,而使用java如何来实现相同的功能呢。 

首先我们先介绍一下一个简单的远程监控程序的实现原理。 

功能一,远程屏幕监视 
(1) 必须要有监控端与被监控端,而且程序保持启动。 
(2) 被监控端获取本机的屏幕截屏发图给监控端。 
(3) 监控端在本地窗口中显示被监控端发送过来的图像。 
(4) (2)(3)步骤重复执行,这时在监控端即可实时监视到被监控端的桌面操作了。 

功能二,远程控制 
(1) 必须要有监控端与被监控端,而且程序保持启动。 
(2) 在监控端监视窗体上执行鼠标点击事件。 
(3) 记录步骤 (2)中的鼠标点击的坐标,及键值发送到被监控端。 
(4) 被监控接受鼠标坐标,及键值,然后再本地屏幕上模拟同样的点击动作。 

OK,现在看下具体的java与语言是如何实现上述功能的。 

使用java语言要实现截屏的功能就要依靠java类库中的一个有趣的类 
java.awt.Robot类【俗称Java机器人】了 


功能一,远程屏幕监视 
//『客户端』抓取屏幕快照GuiCamera.java

 

            BufferedImage screenshot =
                    (new Robot()).createScreenCapture(
                            new Rectangle(0, 0, (int) size.getWidth(),
                                          (int) size.getHeight()));
          
[java]  view plain copy
 
  1. BufferedImage screenshot =  
  2.         (new Robot()).createScreenCapture(  
  3.                 new Rectangle(00, (int) size.getWidth(),  
  4.                               (int) size.getHeight()));  



//『客户端』发送快照 SendThread.java

 
             image=gc.snapShot();
               //保存为临时文件
               File file=new File("temp.png");
               FileOutputStream fileout=new FileOutputStream(file);
               ImageIO.write(image,"png",fileout);
               fileout.close();
               
               //读取图像
               FileInputStream fileIn=new FileInputStream(file);
               int len=(int)file.length();

              //建立字节数组
               byte[] buf=new byte[len];
               fileIn.read(buf,0,len);

               //发送
               out.write(buf,0,len);
               out.flush();
               
               //间隔500毫秒
               Thread.sleep(500);
            
[java]  view plain copy
 
  1. image=gc.snapShot();  
  2.   //保存为临时文件  
  3.   File file=new File("temp.png");  
  4.   FileOutputStream fileout=new FileOutputStream(file);  
  5.   ImageIO.write(image,"png",fileout);  
  6.   fileout.close();  
  7.     
  8.   //读取图像  
  9.   FileInputStream fileIn=new FileInputStream(file);  
  10.   int len=(int)file.length();  
  11.   
  12.  //建立字节数组  
  13.   byte[] buf=new byte[len];  
  14.   fileIn.read(buf,0,len);  
  15.   
  16.   //发送  
  17.   out.write(buf,0,len);  
  18.   out.flush();  
  19.     
  20.   //间隔500毫秒  
  21.   Thread.sleep(500);  




//『监控端』接受图像,Snap.java 

 public void run() {
 while (flag) {
                    byte[] buf = new byte[102400];
                    try {

                        imgStream = new BufferedInputStream(
                                socket.getInputStream());
                        imgStream.read(buf);
                        ImageIcon icon = new ImageIcon(Toolkit.
                                getDefaultToolkit().
                                createImage(buf));
                        lab.setIcon(icon);

                        File file = new File("1.jpg");
                        FileOutputStream fileOut = new FileOutputStream(file);
                        fileOut.write(buf);
                        fileOut.close();

                        repaint();
                        setVisible(true);
                        System.out.println("读取图象成功!");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        flag = false;
                    }
                }
                System.out.println("服务器停止");
            }
}


[java]  view plain copy
 
  1. public void run() {  
  2. while (flag) {  
  3.                    byte[] buf = new byte[102400];  
  4.                    try {  
  5.   
  6.                        imgStream = new BufferedInputStream(  
  7.                                socket.getInputStream());  
  8.                        imgStream.read(buf);  
  9.                        ImageIcon icon = new ImageIcon(Toolkit.  
  10.                                getDefaultToolkit().  
  11.                                createImage(buf));  
  12.                        lab.setIcon(icon);  
  13.   
  14.                        File file = new File("1.jpg");  
  15.                        FileOutputStream fileOut = new FileOutputStream(file);  
  16.                        fileOut.write(buf);  
  17.                        fileOut.close();  
  18.   
  19.                        repaint();  
  20.                        setVisible(true);  
  21.                        System.out.println("读取图象成功!");  
  22.                    } catch (Exception ex) {  
  23.                        ex.printStackTrace();  
  24.                        flag = false;  
  25.                    }  
  26.                }  
  27.                System.out.println("服务器停止");  
  28.            }  



功能二,远程控制 


『监控端』记录鼠标操作Snap.java


//内部类,主要功能监听鼠标事件。记录坐标。
class keyAdapet extends KeyAdapter
    { //键盘监听适配器
        public void keyTyped(KeyEvent e) {

            if (e.getKeyChar() == 27) { //按ESC键
                Object[] options = {
                                   "确定",
                                   "取消"};
                int n = JOptionPane.showOptionDialog(null,
                        "是否退出程序?",
                        "远程监控系统",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null, //don't use a custom Icon
                        options, //the titles of buttons
                        options[0]);
                if (0 == n) {
                    System.exit(0);
                }
            }

        }
    }

      
        public void mouseClicked(MouseEvent e) {

            System.out.println("双击了鼠标");
            int x = e.getX();
            int y = e.getY();
            if (tempSocket != null) {
                new CommandMsg("2", tempSocket, x, y).start();
            }
        }

        public void mousePressed(MouseEvent e) {
            if (e.BUTTON1 == MouseEvent.BUTTON1) {
                System.out.println("你按了鼠标左键~~~~~~~~~~~");
                int x = e.getX();
                int y = e.getY();
                if (tempSocket != null) {
                    new CommandMsg("3", tempSocket, x, y).start();
                }
            }
        }

       ......
   }


[java]  view plain copy
 
  1. //内部类,主要功能监听鼠标事件。记录坐标。  
  2. class keyAdapet extends KeyAdapter  
  3.     { //键盘监听适配器  
  4.         public void keyTyped(KeyEvent e) {  
  5.   
  6.             if (e.getKeyChar() == 27) { //按ESC键  
  7.                 Object[] options = {  
  8.                                    "确定",  
  9.                                    "取消"};  
  10.                 int n = JOptionPane.showOptionDialog(null,  
  11.                         "是否退出程序?",  
  12.                         "远程监控系统",  
  13.                         JOptionPane.OK_CANCEL_OPTION,  
  14.                         JOptionPane.QUESTION_MESSAGE,  
  15.                         null//don't use a custom Icon  
  16.                         options, //the titles of buttons  
  17.                         options[0]);  
  18.                 if (0 == n) {  
  19.                     System.exit(0);  
  20.                 }  
  21.             }  
  22.   
  23.         }  
  24.     }  
  25.   
  26.         
  27.         public void mouseClicked(MouseEvent e) {  
  28.   
  29.             System.out.println("双击了鼠标");  
  30.             int x = e.getX();  
  31.             int y = e.getY();  
  32.             if (tempSocket != null) {  
  33.                 new CommandMsg("2", tempSocket, x, y).start();  
  34.             }  
  35.         }  
  36.   
  37.         public void mousePressed(MouseEvent e) {  
  38.             if (e.BUTTON1 == MouseEvent.BUTTON1) {  
  39.                 System.out.println("你按了鼠标左键~~~~~~~~~~~");  
  40.                 int x = e.getX();  
  41.                 int y = e.getY();  
  42.                 if (tempSocket != null) {  
  43.                     new CommandMsg("3", tempSocket, x, y).start();  
  44.                 }  
  45.             }  
  46.         }  
  47.   
  48.        ......  
  49.    }  



『监控端』发送坐标Snap.java 

public void run() {
                out.println(eventType + "," + x + "," + y);
                out.flush();
}


[java]  view plain copy
 
  1. public void run() {  
  2.                 out.println(eventType + "," + x + "," + y);  
  3.                 out.flush();  
  4. }  




『客户端』获取鼠标坐标后,在本机相同坐标位置模拟一个鼠标点击操作 Coop.java

public void run() {
        while (flag) {
            try {
                String s = in.readLine();
                decode(s);
                switch (method) {
                //这里的man实际也是Robot的一个实例。
                case 1:
                    man.mouseMove(x, y);
                    break;
                case 2:
                    man.mouseMove(x, y);
                    man.mousePress(InputEvent.BUTTON1_MASK);
                    man.mouseRelease(InputEvent.BUTTON1_MASK);
                    break;
                case 3:
                    man.mousePress(InputEvent.BUTTON1_MASK);
                    break;
                case 4:
                    man.mouseRelease(InputEvent.BUTTON1_MASK);
                    break;
                default:
                    break;
                }

            } catch (IOException exe) {
                ThreadInfo.CoopIsLive=false;
                flag=false;
                exe.printStackTrace();
            }
        }
    }

[java]  view plain copy
 
  1. public void run() {  
  2.         while (flag) {  
  3.             try {  
  4.                 String s = in.readLine();  
  5.                 decode(s);  
  6.                 switch (method) {  
  7.                 //这里的man实际也是Robot的一个实例。  
  8.                 case 1:  
  9.                     man.mouseMove(x, y);  
  10.                     break;  
  11.                 case 2:  
  12.                     man.mouseMove(x, y);  
  13.                     man.mousePress(InputEvent.BUTTON1_MASK);  
  14.                     man.mouseRelease(InputEvent.BUTTON1_MASK);  
  15.                     break;  
  16.                 case 3:  
  17.                     man.mousePress(InputEvent.BUTTON1_MASK);  
  18.                     break;  
  19.                 case 4:  
  20.                     man.mouseRelease(InputEvent.BUTTON1_MASK);  
  21.                     break;  
  22.                 default:  
  23.                     break;  
  24.                 }  
  25.   
  26.             } catch (IOException exe) {  
  27.                 ThreadInfo.CoopIsLive=false;  
  28.                 flag=false;  
  29.                 exe.printStackTrace();  
  30.             }  
  31.         }  
  32.     }  

 

代码的部分就介绍到这里,由于java语言的一些限制,本实例仅作为演示。有感兴趣的朋友可以下载附件中的程序做进一步参考。远程监控2.zip (227.9 KB)

 

原帖地址 http://www.javaeye.com/topic/200963

你可能感兴趣的:(web Service)