📄 servertest.java
字号:
/****************************************
* 程序文件名称: ServerTest.java
* 功能: 服务器应用程序
*****************************************/
import java.awt.*;
import java.awt.event.*;
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;
import java.util.zip.*;
//import java.awt.geom.*;
//import javax.imageio.*;
//import java.util.*;
public class ServerTest extends Thread
{
private DatagramSocket socket; //UDP
public static final int PORT=5000;
public static final int MAX=4096000; //数据大小
public boolean end;
private Robot robot;
private Toolkit toolkit;
int i=0;
int[] pixels1,pixels2,pixels3,pixels4;
int x;
//SocketAddress address=SocketAddress.getByName("192.169.1.77");
/* 1.构造方法ServerTest() */
public ServerTest() throws Exception
{
robot=new Robot();
toolkit=Toolkit.getDefaultToolkit();
this.socket=new DatagramSocket(PORT);
socket.setSendBufferSize(MAX);
end=false;
}
/* 2.发送屏幕方法sendScreen() */
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.3f,false);
//JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(output,param);
//encoder.encode(image);
//encoder.getOutputStream().close();
//添加部分
Robot robot=null;
try{ robot=new Robot(); }
catch(Exception er){ }
Rectangle screenRect=null;
int width=toolkit.getScreenSize().width;
int height=toolkit.getScreenSize().height;
screenRect=new Rectangle(0,0,width,height);
//window.setVisible(false);
//this.window.setVisible(false);
//robot.delay(500);
BufferedImage image=robot.createScreenCapture(screenRect);
//cropped = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
pixels1=new int[width*height];
//pixels2=new int[width*height];
pixels3=new int[width*height];
image.getRGB(0,0,width,height,pixels1,0,width);
//System.out.println(pixels1);
//ImageProducer cl=new MemoryImageSource(width,height,pixels1,0,width);
//cropped=createImage(cl);
//for(x=0;x<width*height;x++){
//if(x%2==0){pixels1[x]=0;}
//}
//(如果pixels2不为空 pixels1与原pixels2数组比对将pixels1不同的像素放入pixels3中)
if (pixels2 != null ){
System.out.println("pixels2不为空");
int compare;
for(x=0;x<=width*height-1;x++){//width*height-1 ,(width-1)*(height-1)
compare=pixels1[x] ^ pixels2[x];
if ( compare !=0 ){
pixels3[x] = pixels1[x];
pixels2[x] = pixels1[x];
}
}
}else{//(pixels1存入pixels2,pixels3中)
System.out.println("pixels2为空");
pixels2= new int[width*height];
//pixels3= new int[width*height];
for(x=0;x<=width*height-1;x++){
pixels2[x] = pixels1[x];
pixels3[x] = pixels1[x];
}
}
byte[] bytecs = ints2bytes(pixels3);//IntToByte
//byte[] bytt= new byte[]
/* try { //ByteArrayOutputStream output=new ByteArrayOutputStream();
GZIPOutputStream gzout=new GZIPOutputStream(output); //得到压缩输出流
//byte[] gbuf=new byte[1024];//设定读入缓冲区尺寸
gzout.write(bytecs,0,bytecs.length); //写入缓冲数据
System.out.print("bytecs.length=" + bytecs.length);//未压缩的数据大小
gzout.close(); //关闭压缩输出流
System.out.print("output.size()=" + output.size());//压缩的数据大小
}catch (Exception e1111) { e1111.printStackTrace();} */
//添加部分
DatagramPacket packet=
new DatagramPacket(bytecs,bytecs.length,address);
this.socket.send(packet);
System.out.println(++i+"\n");
}
catch (Exception e) { e.printStackTrace();}
}
/* 3.线程方法RUN() */
public void run()
{
//定义字节数组,放置接收到的数据
byte[] bytes=new byte[4096];
while(!end) {
try {
//创建数据包对象,以便收取数据包
DatagramPacket packet=new DatagramPacket(bytes,bytes.length);
//堵塞端口,接收数据包
this.socket.receive(packet);
//取出数据包的内容
String command=new String(packet.getData(),packet.getOffset(),20).trim();
//如果接收到的指令为"REFRESH",
//则将自己屏幕图像发送出去
if(command.equalsIgnoreCase("REFRESH")) {
sendScreen(packet.getSocketAddress());
}
//若指令不是"REFRESH",则是一个要执行的鼠标或键盘动作指令
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());
//如果接收到的指令为"MousePressed",
//则在自己的电脑中执行"按下鼠标左键"的动作指令
if(command.equalsIgnoreCase("MousePressed")) {
robot.mousePress(button);
}
//如果接收到的指令为"MouseReleased",
//则在自己的电脑中执行"释放鼠标左键"的动作指令
else if(command.equalsIgnoreCase("MouseReleased")) {
robot.mouseRelease(button);
}
//如果接收到的指令为"MouseMoved",
//则在自己的电脑中执行"移动鼠标"的动作指令
else if(command.equalsIgnoreCase("MouseMoved")) {
robot.mouseMove(x,y);
}
//如果接收到的指令为"MousePressed",
//则在自己的电脑中执行"滚动鼠标滚轮"的动作指令
else if(command.equalsIgnoreCase("MouseWheel")) {
robot.mouseWheel(button);
}
//如果接收到的指令为"KeyPressed",
//则在自己的电脑中执行"按下按键"的动作指令
else if(command.equalsIgnoreCase("KeyPressed")) {
robot.keyPress(x);
}
//如果接收到的指令为"KeyReleased",
//则在自己的电脑中执行"释放按键"的动作指令
else if(command.equalsIgnoreCase("KeyReleased")){
robot.keyRelease(x);
}
} /* end else */
} /* end try */
catch (Exception e) { }
} /* end while */
} /* end run */
/* 4. CLOSE() 方法 */
public void close()
{
end=true;
this.socket.close();
}
/* 添加 方法 */
//*int[] to byte[]
public static byte[] ints2bytes(int[] iValue)
{
int i,aa=iValue.length;
byte[] bytebuf1=new byte[aa*4];
for( i=0;i<aa;i++){
for(int y=0;y<4;y++){
bytebuf1[i*4+y]=IntToByte(iValue[i])[y];
//System.out.println("bytebuf1=" + bytebuf1[i+y]);
}
}
return bytebuf1;
}
//*int to byte
public static byte[] IntToByte(int nValue) {
int nTemp=0;
byte[] bytebuf=new byte[4];
for(int i=0;i<4;i++){
nTemp=nValue>>(i*8);
Integer IntObj=new Integer(nTemp);
bytebuf[i]=IntObj.byteValue();
//System.out.println("bytebuf[i]=" + bytebuf[i]);
}
return bytebuf;
}
//*byte[] to int[]
public static int[] bytes2ints(byte[] bytebuf1)
{
int len=bytebuf1.length;
int len4=len/4;
//System.out.print("len4=" + len4 + "\n");
int[] ints=new int[len4];
byte[] bytebuf2=new byte[4];
for(int i=0;i<len4;i++){
for(int y=0;y<4;y++){
bytebuf2[y]=bytebuf1[i*4+y];
}
ints[i]=ByteToInt(bytebuf2);
}
return ints ;
}
//*byte to int
public static int ByteToInt(byte[] bytebuf)
{
byte[] buf=new byte[4];
int len=4,i=0;
int nTemp=0,nValue=0;
if(bytebuf.length<len){
len=bytebuf.length;
}
if(len==0){
return -1;
}
for(i=0;i<len;i++){
buf[i]=bytebuf[i];
}
for(i=0;i<4;i++){
nTemp=(int)buf[i];
if(nTemp<0){
nTemp=256+nTemp;
}
//System.out.println(nTemp);
nTemp=nTemp<<(8*i);
nValue=nValue+nTemp;
}
return nValue;
}
/* 5. MAIN()方法 */
public static void main(String[] args)
{
ServerTest one=null;
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
/* 窗体建立对象 */
JFrame frame=new JFrame("受监视中。。。");
frame.getContentPane().setLayout(new BorderLayout());
frame.setSize(240,80);
JButton exit=new JButton("退出");
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);
}//endtry
catch (Exception e)
{
e.printStackTrace();
if(one!=null) { one.close(); }
System.exit(0);
}
} //end main
}//end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -