利用java实现一个简单的远程监控程序
一般的遠(yuǎn)程監(jiān)控軟件都是用c或者c++等語言開發(fā)的,而使用java如何來實(shí)現(xiàn)相同的功能呢。
首先我們先介紹一下一個(gè)簡(jiǎn)單的遠(yuǎn)程監(jiān)控程序的實(shí)現(xiàn)原理。
功能一,遠(yuǎn)程屏幕監(jiān)視
(1) 必須要有監(jiān)控端與被監(jiān)控端,而且程序保持啟動(dòng)。
(2) 被監(jiān)控端獲取本機(jī)的屏幕截屏發(fā)圖給監(jiān)控端。
(3) 監(jiān)控端在本地窗口中顯示被監(jiān)控端發(fā)送過來的圖像。
(4) (2)(3)步驟重復(fù)執(zhí)行,這時(shí)在監(jiān)控端即可實(shí)時(shí)監(jiān)視到被監(jiān)控端的桌面操作了。
功能二,遠(yuǎn)程控制
(1) 必須要有監(jiān)控端與被監(jiān)控端,而且程序保持啟動(dòng)。
(2) 在監(jiān)控端監(jiān)視窗體上執(zhí)行鼠標(biāo)點(diǎn)擊事件。
(3) 記錄步驟 (2)中的鼠標(biāo)點(diǎn)擊的坐標(biāo),及鍵值發(fā)送到被監(jiān)控端。
(4) 被監(jiān)控接受鼠標(biāo)坐標(biāo),及鍵值,然后再本地屏幕上模擬同樣的點(diǎn)擊動(dòng)作。
OK,現(xiàn)在看下具體的java與語言是如何實(shí)現(xiàn)上述功能的。
使用java語言要實(shí)現(xiàn)截屏的功能就要依靠java類庫中的一個(gè)有趣的類
java.awt.Robot類【俗稱Java機(jī)器人】了
功能一,遠(yuǎn)程屏幕監(jiān)視
//『客戶端』抓取屏幕快照GuiCamera.java
BufferedImage screenshot =
(new Robot()).createScreenCapture(
new Rectangle(0, 0, (int) size.getWidth(),
(int) size.getHeight()));
//『客戶端』發(fā)送快照 SendThread.java
image=gc.snapShot();
//保存為臨時(shí)文件
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();
//建立字節(jié)數(shù)組
byte[] buf=new byte[len];
fileIn.read(buf,0,len);
//發(fā)送
out.write(buf,0,len);
out.flush();
//間隔500毫秒
Thread.sleep(500);
image=gc.snapShot();
//保存為臨時(shí)文件
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();
//建立字節(jié)數(shù)組
byte[] buf=new byte[len];
fileIn.read(buf,0,len);
//發(fā)送
out.write(buf,0,len);
out.flush();
//間隔500毫秒
Thread.sleep(500);
//『監(jiān)控端』接受圖像,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("服務(wù)器停止");
}
}
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("服務(wù)器停止");
}
}
功能二,遠(yuǎn)程控制
『監(jiān)控端』記錄鼠標(biāo)操作Snap.java
//內(nèi)部類,主要功能監(jiān)聽鼠標(biāo)事件。記錄坐標(biāo)。
class keyAdapet extends KeyAdapter
{ //鍵盤監(jiān)聽適配器
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 27) { //按ESC鍵
Object[] options = {
"確定",
"取消"};
int n = JOptionPane.showOptionDialog(null,
"是否退出程序?",
"遠(yuǎn)程監(jiān)控系統(tǒng)",
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("雙擊了鼠標(biāo)");
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("你按了鼠標(biāo)左鍵~~~~~~~~~~~");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("3", tempSocket, x, y).start();
}
}
}
......
}
//內(nèi)部類,主要功能監(jiān)聽鼠標(biāo)事件。記錄坐標(biāo)。
class keyAdapet extends KeyAdapter
{ //鍵盤監(jiān)聽適配器
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 27) { //按ESC鍵
Object[] options = {
"確定",
"取消"};
int n = JOptionPane.showOptionDialog(null,
"是否退出程序?",
"遠(yuǎn)程監(jiān)控系統(tǒng)",
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("雙擊了鼠標(biāo)");
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("你按了鼠標(biāo)左鍵~~~~~~~~~~~");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("3", tempSocket, x, y).start();
}
}
}
......
}
『監(jiān)控端』發(fā)送坐標(biāo)Snap.java
public void run() {
out.println(eventType + "," + x + "," + y);
out.flush();
}
public void run() {
out.println(eventType + "," + x + "," + y);
out.flush();
}
『客戶端』獲取鼠標(biāo)坐標(biāo)后,在本機(jī)相同坐標(biāo)位置模擬一個(gè)鼠標(biāo)點(diǎn)擊操作 Coop.java
public void run() {
while (flag) {
try {
String s = in.readLine();
decode(s);
switch (method) {
//這里的man實(shí)際也是Robot的一個(gè)實(shí)例。
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實(shí)際也是Robot的一個(gè)實(shí)例。
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語言的一些限制,本實(shí)例僅作為演示。有感興趣的朋友可以下載附件中的程序做進(jìn)一步參考。 java遠(yuǎn)程監(jiān)控.rar (224.7 KB)
原帖地址 http://www.javaeye.com/topic/200963
總結(jié)
以上是生活随笔為你收集整理的利用java实现一个简单的远程监控程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA复习5(集合——ArrayLis
- 下一篇: 助力教育 苹果对中国发展研究基金会的捐助