在公司,无聊之际,看到网上关于Robot()类的使用,随想自己实现一个简单的监控系统。这个监控系统没有实现远程的控制操作,只是简单的客户端可以看到远程看到服务器的桌面。另外我在机房局域网测试的时候,发现客户端发现服务器的改变需要5-10秒的时间。不知原因为何?
java 代码
- /*
- 客户端的实现:主要实现的功能是从服务器得到服务器桌面的图片,并进行显示。
- */
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.util.*;
- import java.util.Timer;
- import java.io.IOException;
- import java.net.*;
- public class Client extends JFrame
- {
- private java.net.Socket cs;
- private MyPanel mypanel;
- public Client()
- {
- mypanel=new MyPanel();
- this.getContentPane().add(mypanel);
- this.clock.start();
- this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
- }
- public static void main(String[] args)
- {
- Client client = new Client();
- client.setSize(800, 800);
- client.setVisible(true);
- }
- private javax.swing.Timer clock=new javax.swing.Timer(1000,new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- cs=new java.net.Socket("192.168.0.3", 12000);
- Image image=javax.imageio.ImageIO.read(cs.getInputStream());
- mypanel.setImage(image);
- mypanel.repaint();
- cs.close();
- } catch (UnknownHostException e2)
- {
- e2.printStackTrace();
- } catch (IOException e2)
- {
- e2.printStackTrace();
- }
- }
- });
- public void drawImage()
- {
- }
- }
- class MyPanel extends JPanel
- {
- private Image image;
- public MyPanel(Image image)
- {
- this.image=image;
- }
- public void setImage(Image image)
- {
- this.image=image;
- }
- public MyPanel()
- {
- // TODO 自动生成构造函数存根
- }
- public void paintComponent(Graphics g)
- {
- g.drawImage(image, 0, 0, null);
- }
- }
- /***********************************************************************
- *************************************************************************/
- /*
- 服务器端的实现:
- 主要实现采集服务器的桌面,并多线程实现给客户端的发送
- */
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server
- {
- private final static int PORT = 12000;
- public static void main(String[] args)
- {
- try
- {
- ServerSocket server = new ServerSocket(PORT);
- Socket connection = null;
- while (true)
- {
- try
- {
- new ShotThread(server).start();
- } finally
- {
- try
- {
- if (connection != null)
- connection.close();
- } catch (IOException e)
- {
- }
- }
- }
- } catch (IOException ee)
- {
- System.err.println(ee);
- }
- } // end main
- }
- /**
- * 多线程的连接:
- *
- */
- class ShotThread extends Thread
- {
- ServerSocket server;
- public ShotThread(ServerSocket server)
- {
- try
- {
- this.connection = server.accept();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public void run()
- {
- System.out.println("得到连接:");
- // try
- // {
- // // this.sleep(500);
- // } catch (InterruptedException e2)
- // {
- // //
- // e2.printStackTrace();
- // }
- java.awt.image.BufferedImage image = (new ShotImage()).snapShot();
- if (image == null)
- try
- {
- throw new IOException();
- } catch (IOException e1)
- {
- // TODO 自动生成 catch 块
- e1.printStackTrace();
- }
- try
- {
- javax.imageio.ImageIO.write(image, "jpg", connection
- .getOutputStream());
- connection.close();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- Socket connection = null;
- }
- /************************************************************************
- *************************************************************************/
- /*
- 图片采集的代码
- */
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.image.BufferedImage;
- public class ShotImage
- {
- private Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
- private String imageFormat = "jpg";
- private int Num = 0;
- BufferedImage screenshot;
- public synchronized BufferedImage snapShot()
- {
- try
- {
- screenshot = (new Robot()).createScreenCapture(new Rectangle(0, 0,
- (int) dimension.getWidth(), (int) dimension.getHeight()));
- } catch (Exception ex)
- {
- System.out.println(ex);
- }
- return screenshot;
- }
- }