局域网电脑远程监控程序
包括服务器端和客户端
在一台机器运行服务器程序
客户端运行客户端程序
可以动态监视服务器的屏幕
能将鼠标和键盘事件传过去,能进行一般的操作
运行环境:jdk1.4.1(能处理鼠标滚轮的事件)
我在三台机器分别是
win2000 ad server
winXP profession
redhat Linux 8.0
上进行了测试,都可以运行服务器端和客户端,并能工作
支持一台服务器多台客户端,大家一起操纵,呵呵
服务器端源代码:
//package com.zms.remotecontrol;
import Java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ServerTest extends Thread {
private DatagramSocket socket;
public static final int PORT=5000;
public static final int MAX=102400;
public boolean end;
private Robot robot;
private Toolkit toolkit;
public ServerTest() throws Exception {
robot=new Robot();
toolkit=Toolkit.getDefaultToolkit();
this.socket=new DatagramSocket(PORT);
socket.setSendBufferSize(MAX);
end=false;
}
private void sendScreen(SocketAddress address) {
try {
BufferedImage image=robot.createScreenCapture(new Rectangle(toolkit.getScreenSize()));
ByteArrayOutputStream output=new ByteArrayOutputStream();
JPEGEncodeParam param=JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(0.1f,false);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(output,param);
encoder.encode(image);
encoder.getOutputStream().close();
DatagramPacket packet=new DatagramPacket(output.toByteArray(),output.size
(),address);
this.socket.send(packet);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
byte[] bytes=new byte[1020];
while(!end) {
try {
DatagramPacket packet=new DatagramPacket(bytes,bytes.length);
this.socket.receive(packet);
String command=new String(packet.getData(),packet.getOffset(),20).trim();
if(command.equalsIgnoreCase('REFRESH')) {
sendScreen(packet.getSocketAddress());
} else {
byte[] the=packet.getData();
int n=packet.getOffset();
int x=Integer.parseInt(new String(the,n+20,10).trim());
int y=Integer.parseInt(new String(the,n+30,10).trim());
int button=Integer.parseInt(new String(the,n+40,10).trim());
if(command.equalsIgnoreCase('MousePressed')) {
robot.mousePress(button);
} else if(command.equalsIgnoreCase('MouseReleased')) {
robot.mouseRelease(button);
} else if(command.equalsIgnoreCase('MouseMoved')) {
robot.mouseMove(x,y);
} else if(command.equalsIgnoreCase('MouseWheel')) {
robot.mouseWheel(button);
} else if(command.equalsIgnoreCase('KeyPressed')) {
robot.keyPress(x);
} else if(command.equalsIgnoreCase('KeyReleased')) {
robot.keyRelease(x);
}
}
} catch (Exception e) {
try {
Thread.sleep(100);
} catch (Exception ex) {
}
}
}
}
public void close() {
end=true;
this.socket.close();
}
public static void main(String[] args) {
ServerTest one=null;
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame=new JFrame('Remote Control Server');
frame.getContentPane().setLayout(new BorderLayout());
frame.setSize(240,80);
JButton exit=new JButton('Exit');
frame.getContentPane().add(exit,BorderLayout.CENTER);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
Dimension a=frame.getSize();
frame.setLocation((screen.width-a.width)/2,(screen.height-a.height)/2);
one=new ServerTest();
one.start();
final ServerTest the=one;
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
the.close();
System.exit(0);
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
the.close();
System.exit(0);
}
});
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
if(one!=null) {
one.close();
}
System.exit(0);
}
}
}
客户端源程序已经编写了,正在完善中。欢迎大家来信咨询,在我的主页已经公开源代码了!
//客户端源代码:
/*
* Created on 2003-3-25
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
使用方法:
先在服务器运行服务器端程序
再在客户端运行客户端程序(不要运行在一台机器上,你会发现鼠标很希奇)
在客户端右击中间区域,单击那个菜单项,输入服务器的地址
ok
让我继续来完善
*/