Java开发的屏幕监控源代码也可以在Linux上使用
本案例分两个类Server类和Client类,其余的类为内部类。该案例纯属Java编程。
并且已经在Linux上测试通过。
屏幕监视设计思想:http://www.blackswansoft.com/songhaikang/article/20110221105156640.html
-------------------------Server类---------------------------------------
import 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.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/**
*使用Java截屏工具,不停的截取当前屏幕图片,图片不需保存直接以流的形式发送的监控端电脑上,并显示出来
*控制端的鼠标和键盘的操作再发送的被控端并且执行从而实现屏幕监控
*可以达到用一台电脑完全控制另外一台电脑
*/
public class Server{
public static void main(String args[]) {
SendScreenImg sender=new SendScreenImg();
sender.changeServerPort(30009);//此处可以修改服务端口
new Thread(sender).start();//打开图像传输服务
OperateWindow operate=new OperateWindow();
// operate.changeServerPort(30010);//此处可以修改服务端口
new Thread(operate).start();//打开主机操控服务
//***** 当然 服务器端的端口修改是随时都可以操作的 它实际上是关闭以前的端口 再开启一个新端口 *****//
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 开启一个设定端口的服务,该服务用于向客户端传送主机的屏幕信息,实现客户端对服务器端主机的监控
* 实例化线程类后默认打开DEFAULT_SERVER_PORT=30011 端口实现监听
* 可以通过changeServerPort改变监听端口,也可以通过getServerPort来查询当前监听端口
*/
class SendScreenImg implements Runnable{
public static final int DEFAULT_SERVER_PORT=30011;
private int serverPort;
private Robot robot;
private ServerSocket serverSocket;
private Rectangle rect;
private Dimension screen;
private BufferedImage img;
private Socket socket;
private ZipOutputStream zip;
public SendScreenImg() {
this.serverPort=SendScreenImg.DEFAULT_SERVER_PORT;
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize();
rect = new Rectangle(screen);
}
public void changeServerPort(int serverPort){
if(this.serverPort==serverPort)return;
this.serverPort=serverPort;
try {
this.serverSocket.close();
} catch (Exception e) {}
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public int getServerPort(){
return this.serverPort;
}
public void run() {
while (true) {
try {
System.out.println("等待接收截屏信息");
socket = serverSocket.accept();
zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9);//为后续的 DEFLATED 条目设置压缩级别 压缩级别 (0-9)
try {
img = robot.createScreenCapture(rect);
&nb