Recently in the course of software engineering design, do a screen monitoring system for the laboratory, reference to a variety of previous code, and finally understand to convert their own code, beginners are so imitated.
Speaking of screen monitoring system, there are teachers and students, the teacher is the server side, the student side to do client side. System in a more interesting place should be a screen broadcast and screen monitoring it, the rest of the name registration, lock screen, timed off, it is relatively simple point.
Screen broadcast, in the functional realization above, plainly, is the teacher's machine constantly intercepting screen information, in the form of pictures sent to each student's computer above, so that students can see the teacher on the computer operation, which is called screen broadcast.
There is a problem in this area, that is, when the screen image is intercepted, there is no mouse information. However, there are two ways to solve this problem:
① when sending a message, draw a mouse over the picture so that there will be two mice on the student's side and the student can move the mouse on his or her computer.
② send the teacher-side mouse coordinates to the student, the student's computer mouse according to the coordinate information real-time movement, here is actually related to the function of control, the student can not move the mouse.
Screen monitoring relatively tricky point, in fact this is this contains two functions: ① teachers to monitor all students computer screen function; ② teacher controls a student's computer; because it involves concurrency, each client has to be in real time to send the screen information to the teacher, it will be a bit troublesome, but still can be achieved.
Temporarily realizes the screen sharing function without the mouse, is relatively simple, needs to be perfected, but can be used as a tool class in the later integration.
The first is the teacher-side server:
Package Test;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.net.serversocket;import Java.net.socket;import Java.util.zip.zipentry;import Java.util.zip.ZipOutputStream; Import javax.imageio.imageio;/* * Ly 2014-11-20 * This kind of real-time send screenshot disappears, multi-threaded implementation, does not contain mouse information, and does not do the optimization of each client processing */publicclass SENDSCREENIMG extends thread{public static int serverport=8000;private serversocket serversocket;private Robot Robot; Public Dimension screen;public Rectangle rect;p rivate socket socket; public static void Main (String args[]) {new sendscreenimg (ServerPort). Start (); Construction method Open socket Connection robot robot get screen size public sendscreenimg (int serverport) {try {serversocket = new ServerSocket (serverport ); Serversocket.setsotimeout (864000000); robot = new Robot ();} catch (Exception e) {e.printstacktrace ();} Screen = Toolkit.getdefaulttoolkit (). Getscreensize (); Gets the size of the home screen rect = new Rectangle (screen); Construct the screen-sized rectangle} @Overridepublic void Run () {//real-time waiting to receive the screenshot message while (true) {Try{socket = Serversocket.accept (); SYSTEM.OUT.PRINTLN ("Student port is already connected"); Zipoutputstream zip = new Zipoutputstream (New DataOutputStream (Socket.getoutputstream ())); Zip.setlevel (9); Set the compression level bufferedimage img = robot.createscreencapture (rect); Zip.putnextentry (New ZipEntry ("test.jpg")); Imageio.write (img, "JPG", zip); if (zip!=null) zip.close (); SYSTEM.OUT.PRINTLN ("Client is connecting in real time"); } catch (IOException IoE) {System.out.println ("disconnected"); } finally {if (socket! = NULL) {try {socket.close (); } catch (IOException e) {e.printstacktrace ();} } } }}}
Then the student-side client:
Package Test;import Java.awt.frame;import Java.awt.image;import java.awt.event.windowadapter;import Java.awt.event.windowevent;import Java.io.datainputstream;import Java.io.ioexception;import Java.net.Socket; Import Java.util.concurrent.timeunit;import Java.util.zip.zipinputstream;import Javax.imageio.imageio;import Javax.swing.imageicon;import javax.swing.jframe;import javax.swing.jlabel;/* * Ly 2014-11-20 * This class is used to receive the teacher-side screen information, excluding the mouse, Pending optimization */public class Receiveimages extends Thread{public borderinit frame;p ublic Socket socket;public String Ip;public St atic void Main (string[] args) {new receiveimages (new Borderinit (), "127.0.0.1"). Start (); Public Receiveimages (Borderinit frame,string IP) {this.frame = Frame;this. Ip=ip;} public void Run () {while (Frame.getflag ()) {try {socket = new socket (ip,8000);D atainputstream imginput = new Datainputstrea M (Socket.getinputstream ()); Zipinputstream imgzip = new Zipinputstream (imginput); Imgzip.getnextentry (); To the beginning of the zip file stream, image img = Imageio.read (imGZIP); Reads the picture Frame.jlbImg.setIcon (New ImageIcon (IMG)) inside the zip image stream in bytes; SYSTEM.OUT.PRINTLN ("Connection" + (System.currenttimemillis ()/1000)%24%60+ "SEC"); Frame.validate (); TimeUnit.MILLISECONDS.sleep (50);//Receive Picture interval time imgzip.close ();} catch (IOException | Interruptedexception e) {System.out.println ("Disconnected");} Finally{try {socket.close ();} catch (IOException e) {}}}}}//client-side window helper class, specifically used to display the screen information received from the teacher class Borderinit extends Jframe{private static final Long serialversionuid = 1l;public JLabel jlbimg;private boolean flag;public boolean Getflag () { return This.flag;} Public Borderinit () {this.flag=true;this.jlbimg = new JLabel (); This.settitle ("Remote monitoring--ip:" + "--topic:"); This.setsize (400, //this.setundecorated (TRUE); Full screen display, it is best to comment out//this.setalwaysontop (true) when testing; The display window is always at the front this.add (jlbimg); This.setlocationrelativeto (null); This.setextendedstate (Frame.maximized_both); This.setdefaultcloseoperation (Dispose_on_close); this.setvisible (true); This.validate ();// Window Close Event This.addwindowlistener (new Windowadapter () {public void WIndowclosing (WindowEvent e) {flag=false; BorderInit.this.dispose (); System.out.println ("form off"); System.GC (); Garbage collection});}}
Very late, never finished in the extraction of such a small function, because the promise of a child shoes to link, the distance from the finished product has a lot to write, follow up on it.
Java implementation of screen sharing applet