利用java实现一个简单的远程监控程序
一般的遠程監控軟件都是用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()));
//『客戶端』發送快照 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
總結
以上是生活随笔為你收集整理的利用java实现一个简单的远程监控程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA复习5(集合——ArrayLis
- 下一篇: JAVA复习5(集合—— Vetor)