- 浏览: 33207 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
handonghandong:
你傻吗? 这是源码吗?
用java也能开发出如此强悍的远程控制软件,而且还开源 -
songhaikang:
depravedangel 写道没有看到源码的连接,下载下来的 ...
Java开发出的远程控制软件 -
songhaikang:
depravedangel 写道看到了,只能算是共享软件吧,不 ...
Java开发出的远程控制软件 -
depravedangel:
看到了,只能算是共享软件吧,不能称之为开源,呵呵,不过不错,比 ...
Java开发出的远程控制软件 -
depravedangel:
没有看到源码的连接,下载下来的是jar
Java开发出的远程控制软件
本案例分两个类Server类和Client类,其余的类为内部类。该案例纯属Java编程。
并且已经在Linux上测试通过。
屏幕监视设计思想:http://www.blackswansoft.com/songhaikang/article/20110221105156640.html
-------------------------Server类---------------------------------------
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/**
*使用Java截屏工具,不停的截取当前屏幕图片,图片不需保存直接以流的形式发送的监控端电脑上,并显示出来
*控制端的鼠标和键盘的操作再发送的被控端并且执行从而实现屏幕监控
*可以达到用一台电脑完全控制另外一台电脑
*/
public class Server{
public static void main(String args[]) {
SendScreenImg sender=new SendScreenImg();
sender.changeServerPort(30009);//此处可以修改服务端口
new Thread(sender).start();//打开图像传输服务
OperateWindow operate=new OperateWindow();
// operate.changeServerPort(30010);//此处可以修改服务端口
new Thread(operate).start();//打开主机操控服务
//***** 当然 服务器端的端口修改是随时都可以操作的 它实际上是关闭以前的端口 再开启一个新端口 *****//
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 开启一个设定端口的服务,该服务用于向客户端传送主机的屏幕信息,实现客户端对服务器端主机的监控
* 实例化线程类后默认打开DEFAULT_SERVER_PORT=30011 端口实现监听
* 可以通过changeServerPort改变监听端口,也可以通过getServerPort来查询当前监听端口
*/
class SendScreenImg implements Runnable{
public static final int DEFAULT_SERVER_PORT=30011;
private int serverPort;
private Robot robot;
private ServerSocket serverSocket;
private Rectangle rect;
private Dimension screen;
private BufferedImage img;
private Socket socket;
private ZipOutputStream zip;
public SendScreenImg() {
this.serverPort=SendScreenImg.DEFAULT_SERVER_PORT;
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize();
rect = new Rectangle(screen);
}
public void changeServerPort(int serverPort){
if(this.serverPort==serverPort)return;
this.serverPort=serverPort;
try {
this.serverSocket.close();
} catch (Exception e) {}
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public int getServerPort(){
return this.serverPort;
}
public void run() {
while (true) {
try {
System.out.println("等待接收截屏信息");
socket = serverSocket.accept();
zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9);//为后续的 DEFLATED 条目设置压缩级别 压缩级别 (0-9)
try {
img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img, "jpg", zip);
if(zip!=null)zip.close();
System.out.println("被控端:connect");
} catch (IOException ioe) {
System.out.println("被控端:disconnect");
}
} catch (IOException ioe) {
System.out.println("错误1");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 开启一个设定端口的服务,该服务用于接受客户端传来的操作字符串,实现对服务器端主机的操控
* 实例化线程类后默认打开DEFAULT_SERVER_PORT=30012 端口实现监听
* 可以通过changeServerPort改变监听端口,也可以通过getServerPort来查询当前监听端口
*/
class OperateWindow implements Runnable {
public static final int DEFAULT_SERVER_PORT=30012;
private int serverPort;
private ServerSocket serverSocket;
private Robot robot;
public OperateWindow() {
this.serverPort=OperateWindow.DEFAULT_SERVER_PORT;
try {
this.serverSocket = new ServerSocket(this.serverPort);
this.serverSocket.setSoTimeout(86400000);
} catch (IOException e) {
e.printStackTrace();
}
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void changeServerPort(int serverPort){
if(this.serverPort==serverPort)return;
this.serverPort=serverPort;
try {
this.serverSocket.close();
} catch (Exception e) {}
try {
this.serverSocket = new ServerSocket(this.serverPort);
this.serverSocket.setSoTimeout(86400000);
} catch (Exception e) {
e.printStackTrace();
}
}
public int getServerPort(){
return this.serverPort;
}
public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
//读取操作信息:120,200,InputEvent.BUTTON1_DOWN_MASK 全部是int类型
InputStream is = socket.getInputStream();
int r;
String info = "";
while ((r = is.read()) != -1) {
info += "" + (char) r;
}
System.out.println(info);
is.close();
if (info != null) {
String s[] = info.trim().split(",");
if ("mouseClicked".equals(s[0].trim())) {//operateStr Model: mouseClicked,x,y,type
//由于加上单击事件后,鼠标按下并快速抬起 就设计到按下、抬起、单击 三个事件,将单击变为了双击 不合乎规范 所以 服务端并没有实现单击事件的监听,这里保留 不坐修改
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mousePress(type);
robot.mouseRelease(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS click "+type);
}
}else if("mousePressed".equals(s[0].trim())){//operateStr Model: mousePressed,x,y,type
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mousePress(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS press "+type);
}
}else if("mouseReleased".equals(s[0].trim())){//operateStr Model: mouseReleased,x,y,type
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mouseRelease(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS release "+type);
}
}else if("mouseDragged".equals(s[0].trim())){//operateStr Model: mouseDragged,x,y,type
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
System.out.println("ClientINFO:MOUSE move to "+x+","+y );
}
}else if("mouseMoved".equals(s[0].trim())){
if (s.length == 3) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
System.out.println("ClientINFO:MOUSE move to "+x+","+y);
}
}else if("keyPress".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyPress(keycode);
}
}else if("keyRelease".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyRelease(keycode);
}
}else if("keyTyped".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyPress(keycode);
robot.keyRelease(keycode);
}
}
}
} catch (IOException e) {
System.out.println("error1");
}
}
}
}
--------------------------Client类---------------------------------------
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Client{
public static void main(String args[]) {
ServerGUI sendOrder=new ServerGUI("127.0.0.1", "实时操控");//被监控电脑的ip地址
WriteGUI catchScreen=new WriteGUI(sendOrder);
catchScreen.changePort(30009);//现在可以修改获取主机屏幕信息要访问的端口号
new Thread(catchScreen).start();//启动线程
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定端口的服务,从服务器端读取图像流,生成(刷新)管理面板
* 默认访问的端口为DEFAULT_PORT=30011 端口,
* 可以通过changePort来改变访问端口,也可以通过getPort查看当前访问端口
* 实例化线程类时需要传入一个ServerGUI类型的辅助窗体对象
*/
class WriteGUI extends Thread {
public static final int DEFAULT_PORT=30011;
private int port;
private ServerGUI rec;
/**
* @param rec 辅助窗体对象,可通过实例化获得
*/
public WriteGUI(ServerGUI rec) {
this.port=WriteGUI.DEFAULT_PORT;
this.rec = rec;
}
public void changePort(int port){
this.port=port;
}
public int getPort(){
return this.port;
}
public void run() {
while (rec.getBoo()) {
System.out.println((System.currentTimeMillis()/1000)%24%60);
Socket socket = null;
try {
socket = new Socket(rec.getIP(), this.port);
DataInputStream dis = new DataInputStream(socket.getInputStream());
ZipInputStream zis = new ZipInputStream(dis);
Image image = null;
try {
zis.getNextEntry();// 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
image = ImageIO.read(zis);// 把ZIP流转换为图片
rec.jlabel.setIcon(new ImageIcon(image));
rec.scroll.setViewportView(rec.jlabel);
rec.validate();
} catch (IOException ioe) {}
try{
// dis.close();
zis.close();
}catch (Exception e) {}
try {
TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} catch (IOException ioe) {
} finally {
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定主机的指定端口,向主机发送实例化线程类时传入的操控命令,实现对该主机的操控
* 默认访问服务端口为DEFAULT_PORT=30012 端口,主机IP为实例化线程类时传入的IP
* 可以通过changePort和changeIP来修改访问的端口和主机
* 也可以通过setOperateStr来设置需要发送的操控命令
* 需要注意的是,修改访问端口或主机必须在线程启动之前修改,否则修改无效
*/
class SendOperate extends Thread {
public static int DEFAULT_PORT=30012;
private String ip;
private int port;// 30012
private String operateStr;
public SendOperate(String ip, String operateStr) {
this.ip = ip;
this.port = SendOperate.DEFAULT_PORT;
this.operateStr = operateStr;
}
public void setOperateStr(String operateStr){
this.operateStr=operateStr;
}
public void changePort(int port){
this.port=port;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.ip=ip;
return true;
}
return false;
}
public int getPort(){
return this.port;
}
public String getIP(){
return this.ip;
}
public void run() {
if(this.operateStr==null||this.operateStr.equals("")){
return;
}
// if(this.operateStr.trim().startsWith("mouseMoved")){
// return;
// }
try {
Socket socket = new Socket(this.ip, this.port);
OutputStream os = socket.getOutputStream();
os.write((this.operateStr).getBytes());
os.flush();
socket.close();
System.out.println("INFO: 【SendOperate】ip=" + this.ip + ",port=" + this.port + ",operateStr="" + this.operateStr + "".");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 服务工具类
*/
class UtilServer{
public static boolean checkIp(String ip){
if(ip==null)return false;
String []dps=ip.split("\.");
if(dps.length!=4&&dps.length!=6)return false;
boolean isIp=true;
for (int i = 0; i < dps.length; i++) {
try {
int dp=Integer.parseInt(dps[i]);
if(dp>255||dp< 0){
throw new RuntimeException("error IP");
}
} catch (Exception e) {
isIp=false;
break;
}
}
return isIp;
}
}
/**
* @author LanXJ @doctime 2010-7-8
* serverManage的辅助窗体,内部事件封装了sendOperate的实现
*/
class ServerGUI extends JFrame {
private static final long serialVersionUID = 2273190419221320707L;
JLabel jlabel;
JScrollPane scroll;
private String ip;
private int port;
private boolean boo;
public boolean getBoo(){
return this.boo;
}
public int getPort(){
return this.port;
}
public void changePort(int port){
this.port=port;
}
public String getIP(){
return this.ip;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.setTitle(this.getTitle().replace(this.ip, ip));
this.ip=ip;
return true;
}
return false;
}
protected ServerGUI(String IP, String sub) {
this.boo = true;
this.ip = IP;
this.port=SendOperate.DEFAULT_PORT;
this.setTitle("远程监控--IP:" + IP + "--主题:" + sub);
this.jlabel = new JLabel();
this.scroll = new JScrollPane();
this.scroll.add(this.jlabel);
scroll.addMouseListener(new MouseAdapter() {
/*public void mouseClicked(MouseEvent e) {// getMousePosition()
super.mouseClicked(e);
//由于加上单击事件后,鼠标按下并快速抬起 就设计到按下、抬起、单击 三个事件,将单击变为了双击
//所以不实现单击监听
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseClicked,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}*/
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mousePressed,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
@SuppressWarnings("static-access")
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseReleased,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
scroll.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseDragged,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, operateStr);
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseMoved,"+ x + "," + y;
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
String operateStr ="keyPress,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
String operateStr ="keyReleas,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyTyped(KeyEvent e) {
// super.keyTyped(e);
}
});
this.add(scroll);
this.setAlwaysOnTop(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(100, 75, (int) screenSize.getWidth() - 200, (int) screenSize.getHeight() - 150);
// this.setResizable(false);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 关闭窗体不做任何事
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
boo = false;
ServerGUI.this.dispose();
System.out.println("窗体关闭");
System.gc();
}
});
this.setVisible(true);
this.validate();
}
}
推荐软件
用java开发的一款功能强大的开源的远程控制软件,最近又升级了,添加了许多使用的功能,而且代码也有了很大程度的优化。可以实现生成服务器、修改上线主机名称、屏幕监视,屏幕控制、主动连接、上传文件、下载文件、 远程命令、远程任务管理器、发送警告、相互聊天、换皮肤、版本升级提醒等功能,通过该软件可以实现用一台电脑同时控制多台电脑,也可以多台电脑控制同一台电脑等。
软件官方网站下载地址:http://dl.iteye.com/topics/download/c1b7acd2-c7ad-3753-8929-48424025d711
软件如下图所示:
并且已经在Linux上测试通过。
屏幕监视设计思想:http://www.blackswansoft.com/songhaikang/article/20110221105156640.html
-------------------------Server类---------------------------------------
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/**
*使用Java截屏工具,不停的截取当前屏幕图片,图片不需保存直接以流的形式发送的监控端电脑上,并显示出来
*控制端的鼠标和键盘的操作再发送的被控端并且执行从而实现屏幕监控
*可以达到用一台电脑完全控制另外一台电脑
*/
public class Server{
public static void main(String args[]) {
SendScreenImg sender=new SendScreenImg();
sender.changeServerPort(30009);//此处可以修改服务端口
new Thread(sender).start();//打开图像传输服务
OperateWindow operate=new OperateWindow();
// operate.changeServerPort(30010);//此处可以修改服务端口
new Thread(operate).start();//打开主机操控服务
//***** 当然 服务器端的端口修改是随时都可以操作的 它实际上是关闭以前的端口 再开启一个新端口 *****//
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 开启一个设定端口的服务,该服务用于向客户端传送主机的屏幕信息,实现客户端对服务器端主机的监控
* 实例化线程类后默认打开DEFAULT_SERVER_PORT=30011 端口实现监听
* 可以通过changeServerPort改变监听端口,也可以通过getServerPort来查询当前监听端口
*/
class SendScreenImg implements Runnable{
public static final int DEFAULT_SERVER_PORT=30011;
private int serverPort;
private Robot robot;
private ServerSocket serverSocket;
private Rectangle rect;
private Dimension screen;
private BufferedImage img;
private Socket socket;
private ZipOutputStream zip;
public SendScreenImg() {
this.serverPort=SendScreenImg.DEFAULT_SERVER_PORT;
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize();
rect = new Rectangle(screen);
}
public void changeServerPort(int serverPort){
if(this.serverPort==serverPort)return;
this.serverPort=serverPort;
try {
this.serverSocket.close();
} catch (Exception e) {}
try {
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(86400000);
} catch (IOException e1) {
e1.printStackTrace();
}
}
public int getServerPort(){
return this.serverPort;
}
public void run() {
while (true) {
try {
System.out.println("等待接收截屏信息");
socket = serverSocket.accept();
zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9);//为后续的 DEFLATED 条目设置压缩级别 压缩级别 (0-9)
try {
img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img, "jpg", zip);
if(zip!=null)zip.close();
System.out.println("被控端:connect");
} catch (IOException ioe) {
System.out.println("被控端:disconnect");
}
} catch (IOException ioe) {
System.out.println("错误1");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 开启一个设定端口的服务,该服务用于接受客户端传来的操作字符串,实现对服务器端主机的操控
* 实例化线程类后默认打开DEFAULT_SERVER_PORT=30012 端口实现监听
* 可以通过changeServerPort改变监听端口,也可以通过getServerPort来查询当前监听端口
*/
class OperateWindow implements Runnable {
public static final int DEFAULT_SERVER_PORT=30012;
private int serverPort;
private ServerSocket serverSocket;
private Robot robot;
public OperateWindow() {
this.serverPort=OperateWindow.DEFAULT_SERVER_PORT;
try {
this.serverSocket = new ServerSocket(this.serverPort);
this.serverSocket.setSoTimeout(86400000);
} catch (IOException e) {
e.printStackTrace();
}
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
public void changeServerPort(int serverPort){
if(this.serverPort==serverPort)return;
this.serverPort=serverPort;
try {
this.serverSocket.close();
} catch (Exception e) {}
try {
this.serverSocket = new ServerSocket(this.serverPort);
this.serverSocket.setSoTimeout(86400000);
} catch (Exception e) {
e.printStackTrace();
}
}
public int getServerPort(){
return this.serverPort;
}
public void run() {
while (true) {
try {
Socket socket = serverSocket.accept();
//读取操作信息:120,200,InputEvent.BUTTON1_DOWN_MASK 全部是int类型
InputStream is = socket.getInputStream();
int r;
String info = "";
while ((r = is.read()) != -1) {
info += "" + (char) r;
}
System.out.println(info);
is.close();
if (info != null) {
String s[] = info.trim().split(",");
if ("mouseClicked".equals(s[0].trim())) {//operateStr Model: mouseClicked,x,y,type
//由于加上单击事件后,鼠标按下并快速抬起 就设计到按下、抬起、单击 三个事件,将单击变为了双击 不合乎规范 所以 服务端并没有实现单击事件的监听,这里保留 不坐修改
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mousePress(type);
robot.mouseRelease(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS click "+type);
}
}else if("mousePressed".equals(s[0].trim())){//operateStr Model: mousePressed,x,y,type
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mousePress(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS press "+type);
}
}else if("mouseReleased".equals(s[0].trim())){//operateStr Model: mouseReleased,x,y,type
int type = Integer.parseInt(s[s.length - 1].trim());
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
robot.mouseRelease(type);
System.out.println("ClientINFO:MOUSE move to "+x+","+y+" AND execute TYPE IS release "+type);
}
}else if("mouseDragged".equals(s[0].trim())){//operateStr Model: mouseDragged,x,y,type
if (s.length == 4) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
System.out.println("ClientINFO:MOUSE move to "+x+","+y );
}
}else if("mouseMoved".equals(s[0].trim())){
if (s.length == 3) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
robot.mouseMove(x, y);
System.out.println("ClientINFO:MOUSE move to "+x+","+y);
}
}else if("keyPress".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyPress(keycode);
}
}else if("keyRelease".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyRelease(keycode);
}
}else if("keyTyped".equals(s[0].trim())){
if(s.length==2){
int keycode=Integer.parseInt(s[1]);
robot.keyPress(keycode);
robot.keyRelease(keycode);
}
}
}
} catch (IOException e) {
System.out.println("error1");
}
}
}
}
--------------------------Client类---------------------------------------
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Client{
public static void main(String args[]) {
ServerGUI sendOrder=new ServerGUI("127.0.0.1", "实时操控");//被监控电脑的ip地址
WriteGUI catchScreen=new WriteGUI(sendOrder);
catchScreen.changePort(30009);//现在可以修改获取主机屏幕信息要访问的端口号
new Thread(catchScreen).start();//启动线程
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定端口的服务,从服务器端读取图像流,生成(刷新)管理面板
* 默认访问的端口为DEFAULT_PORT=30011 端口,
* 可以通过changePort来改变访问端口,也可以通过getPort查看当前访问端口
* 实例化线程类时需要传入一个ServerGUI类型的辅助窗体对象
*/
class WriteGUI extends Thread {
public static final int DEFAULT_PORT=30011;
private int port;
private ServerGUI rec;
/**
* @param rec 辅助窗体对象,可通过实例化获得
*/
public WriteGUI(ServerGUI rec) {
this.port=WriteGUI.DEFAULT_PORT;
this.rec = rec;
}
public void changePort(int port){
this.port=port;
}
public int getPort(){
return this.port;
}
public void run() {
while (rec.getBoo()) {
System.out.println((System.currentTimeMillis()/1000)%24%60);
Socket socket = null;
try {
socket = new Socket(rec.getIP(), this.port);
DataInputStream dis = new DataInputStream(socket.getInputStream());
ZipInputStream zis = new ZipInputStream(dis);
Image image = null;
try {
zis.getNextEntry();// 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
image = ImageIO.read(zis);// 把ZIP流转换为图片
rec.jlabel.setIcon(new ImageIcon(image));
rec.scroll.setViewportView(rec.jlabel);
rec.validate();
} catch (IOException ioe) {}
try{
// dis.close();
zis.close();
}catch (Exception e) {}
try {
TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} catch (IOException ioe) {
} finally {
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定主机的指定端口,向主机发送实例化线程类时传入的操控命令,实现对该主机的操控
* 默认访问服务端口为DEFAULT_PORT=30012 端口,主机IP为实例化线程类时传入的IP
* 可以通过changePort和changeIP来修改访问的端口和主机
* 也可以通过setOperateStr来设置需要发送的操控命令
* 需要注意的是,修改访问端口或主机必须在线程启动之前修改,否则修改无效
*/
class SendOperate extends Thread {
public static int DEFAULT_PORT=30012;
private String ip;
private int port;// 30012
private String operateStr;
public SendOperate(String ip, String operateStr) {
this.ip = ip;
this.port = SendOperate.DEFAULT_PORT;
this.operateStr = operateStr;
}
public void setOperateStr(String operateStr){
this.operateStr=operateStr;
}
public void changePort(int port){
this.port=port;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.ip=ip;
return true;
}
return false;
}
public int getPort(){
return this.port;
}
public String getIP(){
return this.ip;
}
public void run() {
if(this.operateStr==null||this.operateStr.equals("")){
return;
}
// if(this.operateStr.trim().startsWith("mouseMoved")){
// return;
// }
try {
Socket socket = new Socket(this.ip, this.port);
OutputStream os = socket.getOutputStream();
os.write((this.operateStr).getBytes());
os.flush();
socket.close();
System.out.println("INFO: 【SendOperate】ip=" + this.ip + ",port=" + this.port + ",operateStr="" + this.operateStr + "".");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 服务工具类
*/
class UtilServer{
public static boolean checkIp(String ip){
if(ip==null)return false;
String []dps=ip.split("\.");
if(dps.length!=4&&dps.length!=6)return false;
boolean isIp=true;
for (int i = 0; i < dps.length; i++) {
try {
int dp=Integer.parseInt(dps[i]);
if(dp>255||dp< 0){
throw new RuntimeException("error IP");
}
} catch (Exception e) {
isIp=false;
break;
}
}
return isIp;
}
}
/**
* @author LanXJ @doctime 2010-7-8
* serverManage的辅助窗体,内部事件封装了sendOperate的实现
*/
class ServerGUI extends JFrame {
private static final long serialVersionUID = 2273190419221320707L;
JLabel jlabel;
JScrollPane scroll;
private String ip;
private int port;
private boolean boo;
public boolean getBoo(){
return this.boo;
}
public int getPort(){
return this.port;
}
public void changePort(int port){
this.port=port;
}
public String getIP(){
return this.ip;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.setTitle(this.getTitle().replace(this.ip, ip));
this.ip=ip;
return true;
}
return false;
}
protected ServerGUI(String IP, String sub) {
this.boo = true;
this.ip = IP;
this.port=SendOperate.DEFAULT_PORT;
this.setTitle("远程监控--IP:" + IP + "--主题:" + sub);
this.jlabel = new JLabel();
this.scroll = new JScrollPane();
this.scroll.add(this.jlabel);
scroll.addMouseListener(new MouseAdapter() {
/*public void mouseClicked(MouseEvent e) {// getMousePosition()
super.mouseClicked(e);
//由于加上单击事件后,鼠标按下并快速抬起 就设计到按下、抬起、单击 三个事件,将单击变为了双击
//所以不实现单击监听
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseClicked,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}*/
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mousePressed,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
@SuppressWarnings("static-access")
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseReleased,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
scroll.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseDragged,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, operateStr);
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseMoved,"+ x + "," + y;
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
String operateStr ="keyPress,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
String operateStr ="keyReleas,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyTyped(KeyEvent e) {
// super.keyTyped(e);
}
});
this.add(scroll);
this.setAlwaysOnTop(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(100, 75, (int) screenSize.getWidth() - 200, (int) screenSize.getHeight() - 150);
// this.setResizable(false);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 关闭窗体不做任何事
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
boo = false;
ServerGUI.this.dispose();
System.out.println("窗体关闭");
System.gc();
}
});
this.setVisible(true);
this.validate();
}
}
推荐软件
用java开发的一款功能强大的开源的远程控制软件,最近又升级了,添加了许多使用的功能,而且代码也有了很大程度的优化。可以实现生成服务器、修改上线主机名称、屏幕监视,屏幕控制、主动连接、上传文件、下载文件、 远程命令、远程任务管理器、发送警告、相互聊天、换皮肤、版本升级提醒等功能,通过该软件可以实现用一台电脑同时控制多台电脑,也可以多台电脑控制同一台电脑等。
软件官方网站下载地址:http://dl.iteye.com/topics/download/c1b7acd2-c7ad-3753-8929-48424025d711
软件如下图所示:
相关推荐
JNI允许Java代码调用本地(非Java)代码,可能是为了优化性能或利用特定的操作系统功能,比如在屏幕监控中可能涉及到的低级别图形操作。 3. "monitor.exe" - 这个是Windows系统的可执行文件,很可能是屏幕监控的应用...
本项目基于Java开发,包含2071个文件,包括JavaScript脚本、SVG图形、CSS样式表、Java源代码、TypeScript脚本、HTML页面、JPG图片、MAP映射文件、WOFF2字体文件、PNG图片。系统实现了基于Java的Linux运维监控工具...
此“Linux下海康监控JAVA源码demo”提供了一个基于Java的示例,使得开发者能够在Linux系统中直接进行相关功能的测试和开发。以下是这个Java源码demo可能涵盖的关键知识点: 1. **Java网络编程**:海康监控设备通常...
通过深入研究"屏幕控制源代码.rar"中的内容,开发者不仅可以掌握屏幕控制的基本技术,还能学习到如何在实际项目中应用这些技术,解决实际问题。同时,对于有志于深入系统级编程和自动化领域的程序员来说,这也是一个...
简单 Java从压缩包中提取文件 1个目标文件 简单 Java存储与读取对象 1个目标文件 如题 Java调色板面板源代码 1个目标文件 摘要:Java源码,窗体界面,调色板 使用Java语言编写的一款用于反映颜色变化的面板,也就是大家...
通常,Java进销存系统会部署在Linux服务器上,使用Tomcat或Jenkins等应用服务器。持续集成/持续部署(CI/CD)工具可以帮助自动化构建、测试和部署流程,提升运维效率。 **总结** Java进销存管理系统通过整合各种业务...
Java作为编程语言,具有“一次编写,到处运行”的特性,使得Java CS应用可以在多种操作系统上运行,如Windows、Linux和Mac OS等。 其次,远程监控系统的核心功能包括数据采集、传输、存储和分析。在Java CS系统中,...
《深入解析Java编写的安卓系统优化软件源代码》 在当今移动互联网时代,安卓系统作为全球最广泛使用的智能手机...通过对源代码的学习和研究,我们可以提升在安卓开发领域的技能,更好地应对性能优化和系统维护的挑战。
Java开发的屏幕照相机是一种基于JAVA编程语言设计的软件,用于捕获计算机屏幕上的图像,类似于我们常见的截图工具。这种软件的独特之处在于它利用了Java的跨平台特性,这意味着无论操作系统是Windows、Mac还是Linux...
Java的“一次编写,到处运行”特性确保了该系统能在多种操作系统上运行,如Windows、Linux或Mac OS。此外,Java丰富的类库和API提供了强大的网络通信支持,这对于构建远程监控系统至关重要。 远程监控系统的核心...
2. **Java解释器(java)**:负责执行已编译的字节码,使得程序可以在任何支持Java的平台上运行。 3. **Java文档生成器(javadoc)**:自动生成API文档,方便开发者理解和使用类库。 4. **Java打包工具(jar)**:...
通过学习和理解"ok6410视频监控源代码",开发者不仅可以掌握视频监控系统的实现,还可以进一步提升在嵌入式系统开发、网络编程、前端交互等方面的能力。这个开源项目为个人学习和企业应用提供了宝贵的资源。
总的来说,"大华视频监控java示例demo(Dahua_Camera).7z"是一个全面的开发资源包,它包含了一个使用IntelliJ IDEA构建的Java项目,使用大华SDK实现了设备搜索、连接等核心功能,并且支持在Linux和Windows两种操作...
在Java技术栈的支持下,电子地图系统具备良好的跨平台性,可以在多种操作系统上运行,包括Windows、Linux、Mac OS等。下面我们将深入探讨该系统的几个关键知识点。 1. **Java基础** - **Java SE**:Java标准版...
Java作为多平台支持的语言,使得电力系统能够在各种操作系统上运行,包括Windows、Linux和Unix等。 在电力行业中,源代码可能涵盖了多个模块,例如: 1. **电网调度**:这部分源代码用于实时监控和调度电力网络,...
首先,我们要明确Java是一种跨平台的编程语言,它的“一次编写,到处运行”特性使得基于Java开发的股票软件可以在多种操作系统上运行,如Windows、Mac OS、Linux等。这为用户提供了极大的便利性和兼容性。 在证券...
Linux以其稳定性和安全性成为了服务器平台的首选,而Java作为跨平台的编程语言,其程序在Linux上运行同样具有广泛的应用。这篇博文将深入探讨如何在Linux系统中配置Java环境并执行Java程序。 首先,确保你的Linux...
"软件监控源代码"是指用于监控应用程序状态的原始编程代码,它能够自动检测软件是否正常运行,一旦发现软件停止,就会自动启动软件,以确保服务的连续性。以下是对这个主题的详细解释: 1. **软件监控的重要性**: ...
综上所述,Tigase Server 7.0.1的源代码不仅是一份技术实现,更是一个学习即时通讯协议、分布式系统设计以及Java编程的宝贵资料。通过对源代码的学习和研究,开发者可以深入理解Jabber/XMPP服务器的工作原理,进而...