局域網電腦遠程監控程序
包括服務器端和客戶端
在一臺機器運行服務器程序
客戶端運行客戶端程序
可以動態監視服務器的屏幕
能將鼠標和鍵盤事件傳過去,能進行一般的操作
運行環境: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
讓我繼續來完善
*/
新聞熱點
疑難解答