⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servertest.java

📁 java写的远程屏幕控制系统,在linux和window之间实现了远程互相控制,当时是为了在校园里实现一套实用级的远程教学系统(和我前面上传的jqq结合),现在没时间做了,请同志们实现我的梦想吧(实现
💻 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; 

public class ServerTest extends Thread 
	{ 
      private DatagramSocket socket; //UDP
      public static final int PORT=5000;   
      public static final int MAX=409600; //数据大小
      public boolean end; 
      private Robot robot; 
      private Toolkit toolkit; 
	  int i=0;
	  //SocketAddress address=SocketAddress.getByName("192.168.1.77");
///////1.构造函数//////      
	   public ServerTest() throws Exception
		   { 
             robot=new Robot(); 
             toolkit=Toolkit.getDefaultToolkit(); 
             this.socket=new DatagramSocket(PORT); 
              socket.setSendBufferSize(MAX); 
              end=false; 
            } 
////////2。发送屏幕函数/////      
	   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(); 
                    DatagramPacket packet=new DatagramPacket(output.toByteArray(),output.size(),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); 
                      //    sendScreen(packet.getSocketAddress());
						      
							   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); } 
							}
	
                             }//endtry
                        catch (Exception e)
							{// try { Thread.sleep(10); } 
						      //catch (Exception ex) {} 
                            } 
                      } //end while
              } //end run
      
////////4。CLOSE 函数//////////		
		public void close() 
			 { end=true; 
               this.socket.close(); 
			  } 
 /////////5。MAIN函数//////////////
 public static void main(String[] args)
	 { 
  ServerTest one=null; 
      try { 
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
              
			  ////////5。1窗体建立函数////////
			  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 + -