Java Realization screen Sharing function Example Analysis _java

Source: Internet
Author: User
Tags garbage collection time interval home screen

This article describes the Java implementation of the screen sharing function method. Share to everyone for your reference. The specific analysis is as follows:

Recently in the course of software engineering design, to do a screen monitoring system for the laboratory, reference to a variety of predecessors code, the final understanding to convert their own code, beginners are such imitation came over.

When it comes to screen monitoring systems, there are teachers and students, and the teacher end is the server side, and the students do the client side. The more interesting place in the system should be regarded as screen broadcast and screen monitoring bar, the rest of what name registration, lock screen, timing shutdown, on the relatively simple point.

Screen broadcast, in the function of the above, frankly speaking, is the teacher-side 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, this is called the screen broadcast.

There is a trouble in this place, is to intercept the screen picture, there is no mouse information. But there are two ways to fix it:

① when sending screenshots, draw a mouse on the picture so that there will be two mice on the student side, and the student can move the mouse on their computer.

② send the mouse coordinates to the student end, the student computer mouse based on the coordinate information real-time movement, here is actually involved in the control function, students can not move the mouse.

Screen monitoring is relatively tricky point, in fact this is the function of this includes two:
① Teachers Monitor the functions of all students ' computer screens;
② teacher controls a student's computer;
Because it involves concurrency, every client has to send screen information to the teacher in real time, which is a bit troublesome, but it can be achieved.

Here temporarily implemented without the mouse screen sharing function, relatively simple, to be improved, but can be used as a tool class in the back integration.

The first is the teacher-side server:

Copy Code code as follows:
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 class real-time send screenshots disappear, multithreading implementation, does not contain mouse information, and did not do to each client to do the optimal processing
*/
public class Sendscreenimg extends Thread
{
public static int serverport=8000;
Private ServerSocket ServerSocket;
Private Robot Robot;
public Dimension screens;
Public Rectangle rect;
private socket socket;

public static void Main (String args[])
{
New Sendscreenimg (ServerPort). Start ();
}

Construct method to 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 (); Get the size of the home screen
Rect = new Rectangle (screen); Rectangle that constructs the screen size
}

@Override
public void Run ()
{
Waiting for a screenshot message in real time
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 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:

Copy Code code as follows:
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 screen information from the teacher side, excluding the mouse, to be optimized
*/
public class Receiveimages extends thread{
public Borderinit frame;
public socket socket;
Public String IP;

public static 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);
DataInputStream imginput = new DataInputStream (Socket.getinputstream ());
Zipinputstream imgzip = new Zipinputstream (imginput);

Imgzip.getnextentry (); At the beginning of the zip file stream
Image img = imageio.read (imgzip); Read the picture in the zip picture stream by byte
Frame.jlbImg.setIcon (New ImageIcon (IMG));
SYSTEM.OUT.PRINTLN ("Connection First" + (System.currenttimemillis ()/1000)%24%60+ "seconds");
Frame.validate ();
TimeUnit.MILLISECONDS.sleep (50);//Receive Picture time interval
Imgzip.close ();

catch (IOException | Interruptedexception e) {
System.out.println ("Disconnected");
}finally{
try {
Socket.close ();
catch (IOException e) {}
}
}
}
}

Client-side Window helper class, designed to display screen information received from the teacher side
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:" + "--Subject:");
This.setsize (400, 400);
This.setundecorated (TRUE); Full screen, best comment out when testing
This.setalwaysontop (TRUE); 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 shutdown events
This.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
Flag=false;
BorderInit.this.dispose ();
System.out.println ("form close");
System.GC (); Garbage collection
}
});
}
}

Here is never finished in the extraction of such a small function, from the finished product there are many to write, interested friends can be improved on this basis.

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.