日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/19 综合教程 22 生活家
生活随笔 收集整理的這篇文章主要介紹了 利用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实现一个简单的远程监控程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。