您的位置:首页 > 编程语言 > Java开发

利用java实现一个简单的远程监控程序

2009-06-09 15:13 1016 查看
一般的远程监控软件都是用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()));

BufferedImage screenshot =
(new Robot()).createScreenCapture(
new Rectangle(0, 0, (int) size.getWidth(),
(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);

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);


//『监控端』接受图像,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("服务器停止");

}

}

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("服务器停止");
}
}


功能二,远程控制

『监控端』记录鼠标操作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();

}

}

}

......

}

//内部类,主要功能监听鼠标事件。记录坐标。
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();
}
}
}

......
}


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

public void run() {

out.println(eventType + "," + x + "," + y);

out.flush();

}

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


『客户端』获取鼠标坐标后,在本机相同坐标位置模拟一个鼠标点击操作 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();

}

}

}

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语言的一些限制,本实例仅作为演示。有感兴趣的朋友可以下载附件中的程序做进一步参考。

java远程监控.rar
(224.7 KB)

原帖地址 http://www.javaeye.com/topic/200963
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: