Java implements screen broadcast by controlling the mouse and java mouse screen Broadcast
In the java screen sharing applet, it is mentioned that no mouse is used to intercept the screen. To see the mouse on the instructor interface, you can intercept the screen, draw the mouse over each one, but because the mouse is captured one by one, it is inevitable that the mouse will have a point card. I wrote a java mouse to control the applet, you can see the mouse demonstration in this way.
The implementation method is quite simple. In the previous two articles, the mouse control and the screen sharing function without the mouse are implemented. It is OK to combine the two functions. The following is a simple analysis.
The server regards SendScreenImg and SendMouseMessage as two tool classes, listening to different ports respectively. Both of them implement the Thread class. We use the ExecutorService class of the Thread pool to control them.
Two ports are used, because we do not know how to send the mouse information and the image information at the moment. We may convert the image to a byte array, place the coordinates of the mouse in front of the array. However, the mouse may be inconsistent, because the speed of transferring the coordinates of the mouse is faster than that of passing images. Well, try again when you are free.
The client analogy is above.
The following code is used:
Server:
Main Program:
/** For screen broadcasting, two tools are called: the class for sending screenshot information and the class for sending mouse information, and the thread pool is used. */Public class BroderCast {public static void main (String [] args) {new BroderCast (); System. out. println ("");} public BroderCast () {ExecutorService exector = executors.newfixedthreadpool(2)20.exector.exe cute (new sendscreenimg({{{exector.exe cute (new SendMouseMessage ());}}
Send:
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;/** tool: Send the screenshot information of the instructor to the student, without mouse information. port 8002 is used. * You can draw the mouse over the picture to be sent. To see the mouse information on the student interface. This function is not yet implemented **/publicclass SendScreenImg extends Thread {public int serverPort = 8002; private ServerSocket serverSocket; private Robot robot; public Dimension screen; public Rectangle rect; private Socket socket; public static void main (String args []) {new SendScreenImg (). start ();} public void changeServerPort (int serverPort) {if (this. serverPort = serverPort) return; try {this. serverSocke T. close (); // It is necessary to close the current port this. serverPort = serverPort; serverSocket = new ServerSocket (this. serverPort); serverSocket. setSoTimeout (8000000);} catch (Exception e) {}} // constructor enabling socket connection Robot robot obtaining screen size public SendScreenImg () {try {serverSocket = new ServerSocket (getServerPort (); serverSocket. setSoTimeout (864000000); robot = new Robot ();} catch (Exception e) {e. printStackTrace ();} screen = Toolkit. getdefatooltoolkit (). GetScreenSize (); // obtain the home screen size rect = new Rectangle (screen); // construct a Rectangle of the corresponding size} @ Overridepublic void run () {// wait for receiving screenshots in real time while (true) {try {socket = serverSocket. accept (); ZipOutputStream zip = new ZipOutputStream (new DataOutputStream (socket. getOutputStream (); zip. setLevel (9); // set the compression level try {BufferedImage img = robot. createScreenCapture (rect); zip. putNextEntry (new ZipEntry ("test.jpg"); ImageIO. write (img, "jpg ", Zip); if (zip! = Null) zip. close (); System. out. println ("Student port connected");} catch (IOException ioe) {System. out. println ("controlled end: disconnect") ;}} catch (IOException ioe) {System. out. println ("connection error");} finally {if (socket! = Null) {try {socket. close () ;}catch (IOException e ){}}}}}}
Send mouse information:
/** Tool class: Get the mouse information and send it to the student */public class SendMouseMessage extends Thread {private int OPERATE_PORT = 8001; private ServerSocket server; private Socket socket; private String operateStr; public static void main (String [] args) {new SendMouseMessage (). start () ;}public SendMouseMessage () {try {server = new ServerSocket (OPERATE_PORT); // JOptionPane. showMessageDialog (null, "listener started");} catch (IOException e1) {e1.printStackTrace () ;}// multithreading listens to the public void run () of the client in a wireless Loop () {while (true) {Point point = MouseInfo. getPointerInfo (). getLocation (); // operateStr = "Movemouse," + point. x + "," + point. y; try {socket = server. accept (); socket. setSoTimeout (1000000); DataOutputStream output = new DataOutputStream (socket. getOutputStream (); output. write (operateStr. getBytes (); output. flush (); // flush the output stream and write all buffered output bytes. close (); // close the output stream and release the resource System. out. println ("INFO:" + operateStr);} catch (IOException e) {System. out. println ("stopped connections"); break; // when the connection is disconnected, the wireless cycle is stopped }}}}
Client:
Main Program:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import com.Tool.OperateMouse;import com.Tool.ReceiveImages;public class ReceiveBroderCast {public ExecutorService exector;public static String IP="202.216.60.9";public static void main(String[] args){new ReceiveBroderCast(IP);}public ReceiveBroderCast(String IP) {exector = Executors.newFixedThreadPool(2);exector.execute(new ReceiveImages(IP));exector.execute(new OperateMouse(IP));}}
Receive:
/** Ly 2014-11-20 * this class is used to receive the screen information of the instructor, excluding the mouse * using socket () */public class ReceiveImages extends Thread {public BorderInit frame; public Socket socket; public String IP; public static void main (String [] args) {new ReceiveImages ("202.216.60.7 "). start ();} public ReceiveImages (String IP) {frame = new BorderInit (); this. IP = IP;} public void run () {while (frame. getFlag () {System. out. println ("connected" + (System. currentTimeMillis ()/1000) % 24% 60 + "seconds"); try {socket = new Socket (IP, 8002); DataInputStream ImgInput = new DataInputStream (socket. getInputStream (); ZipInputStream imgZip = new ZipInputStream (ImgInput); Image img = null; try {imgZip. getNextEntry (); // img = ImageIO at the beginning of the Zip file stream. read (imgZip); // read the image frame in the Zip image stream by byte. jlbImg. setIcon (new ImageIcon (img); frame. validate ();} catch (IOException e) {e. printStackTrace ();} try {imgZip. close ();} catch (IOException e) {System. out. println ("disconnected");} try {TimeUnit. MILLISECONDS. sleep (50); // interval of receiving images} catch (InterruptedException ie) {ie. printStackTrace () ;}} catch (IOException e) {e. printStackTrace ();} finally {try {socket. close () ;}catch (IOException e) {}}} 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,400); // this. setUndecorated (true); // this. setAlwaysOnTop (true); // always at the beginning of this. add (jlbImg); this. setLocationRelativeTo (null); this. setExtendedState (Frame. MAXIMIZED_BOTH); this. setdefaclocloseoperation (DISPOSE_ON_CLOSE); this. setVisible (true); this. validate (); // window close event this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {flag = false; BorderInit. this. dispose (); System. out. println ("form closed"); System. gc (); // garbage collection }});}}
Receive mouse information and control mouse movement:
Import java. awt. AWTException; import java. awt. robot; import java. io. bufferedInputStream; import java. io. bufferedReader; import java. io. dataInputStream; import java. io. IOException; import java. io. inputStream; import java.net. socket; import javax. swing. JOptionPane;/** the control mouse on the student side is the same as that on the instructor side * this class is responsible for receiving mouse information and using robot. the mouseMove () function controls mouse movement */public class OperateMouse extends Thread {public static void main (String [] args ){ New OperateMouse ("202.116.60.7 "). start ();} private Socket socket; public String IP; private int OPERATE_PORT = 8001; private Robot robot; public OperateMouse (String IP) {this. IP = IP;} public void run () {while (true) {try {socket = new Socket (IP, OPERATE_PORT); robot = new Robot (); // obtain the mouse movement information DataInputStream dataIn = new DataInputStream (socket. getInputStream (); String info = ""; int r; while (r = dataIn. read ())! =-1) {info + = "" + (char) r; // convert all elements in the byte array to bytes type} dataIn. close (); System. out. println ("Data Stream disconnected" + info); if (info! = Null) {String s [] = info. trim (). split (","); if ("Movemouse ". equals (s [0]. trim (); {if (s. length = 3) {int x = Integer. parseInt (s [1]. trim (); int y = Integer. parseInt (s [2]. trim (); System. out. println ("output mouse information" + x + "" + y); robot. mouseMove (x, y) ;}}} catch (IOException e) {System. out. println ("disconnected"); break;} catch (AWTException e) {e. printStackTrace ();}}}}
This blog is from lanyou