Sir i have the following code for scren capturing it is capturing in png format but when i am sending it over other m/c it is taking delay of around 16 seconds..i think it is because this line of code sleep(10000); because i am making the thread to sleep for 10sec it is taking too much of time if i reduce the amt from 10000 to 2000,3000 the delay is of 5-6 sec which is manageable but after sending 4-5 snapshots of the screen when i again send the image(screenshot) error occurs i.e "OutOfMemory Error" in heap plz suggest a solution so that i can either clear or reset the heap so that the images(snapshots) are sent without any error continously
code for server:
package main; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.io.*; import java.net.*; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; public class RemoteDesktopServer extends JFrame implements Serializable{ ServerSocket serverSocket; Socket socket; public RemoteDesktopServer() { try { serverSocket = new ServerSocket(2009); while (true) { socket = serverSocket.accept(); new ChatHandeler(socket); } } catch (IOException ex) { ex.printStackTrace(); } } public static void main(String[] args) { RemoteDesktopServer server=new RemoteDesktopServer(); } } //======================================== class ChatHandeler extends Thread { FileInputStream fis; ObjectInputStream ois; ImageIcon img; Image image; File file; JFrame frame; RemoteDesktopServer server; //static Vector<ChatHandeler> clientsVector = new Vector<ChatHandeler>(); public ChatHandeler(Socket s) { //server=new RemoteDesktopServer(); file=new File("Server.png"); frame=new JFrame(); try { //scnr = new Scanner(s.getInputStream()); ois=new ObjectInputStream(s.getInputStream()); //clientsVector.add(this); start(); } catch (IOException ex) { ex.printStackTrace(); } } public void run() { while (true) { try { img = (ImageIcon) ois.readObject(); image= img.getImage(); frame.add(new MyPanel(image), BorderLayout.CENTER); img = null; image = null; Runtime runtime= Runtime.getRuntime(); runtime.gc(); frame.setSize(1300, 800); frame.setVisible(true); try { sleep(10000); } catch (InterruptedException ex) { System.out.println(" sleep exception"); } System.out.println("Read"); } catch (ClassNotFoundException ex) { //ex.printStackTrace(); System.out.println(" Class not found exception"); } catch (IOException ex) { //ex.printStackTrace(); System.out.println("IOException"); } } } } class MyPanel extends JPanel { Image image; public MyPanel(Image image ) { this.image=image; Toolkit kit = Toolkit.getDefaultToolkit( ); image = kit.getImage("client.png"); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.drawImage(image, 10, 10, this); } }
code for client:
package main;
//////client import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.awt.Robot; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import org.omg.SendingContext.RunTime; public class RemoteDesktopClient extends JFrame implements Serializable{ public JButton btn; //====================client Socket mySocket; Scanner scn; PrintStream prntStream; //=====================robot Robot robot; BufferedImage screenShot; ImageIcon imageIcon; String str; FileOutputStream fos; ObjectOutputStream oos; File file; public RemoteDesktopClient() { imageIcon=new ImageIcon(); file=new File("client.png"); btn = new JButton("Send"); try { mySocket = new Socket("192.168.1.101", 2009); new Send(mySocket); robot.mouseMove(1000, 800); setSize(300, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); //==========================client }catch (UnknownHostException ex) { System.out.println("UnKnown Host Exception"); } catch (IOException ex) { System.out.println("IO Exception after unknown"); } //==========================client } public static void main(String[] args) { RemoteDesktopClient client = new RemoteDesktopClient(); } //=========================================== class Send extends Thread { //public Rectangle rectangle; public Send(Socket s) { try { //scn = new Scanner(s.getInputStream()); //rectangle = new Rectangle(); oos = new ObjectOutputStream(mySocket.getOutputStream()); robot = new Robot(); start(); } catch (IOException ex) { ex.printStackTrace(); } catch (AWTException ex) { ex.printStackTrace(); } } public void run() { while (true) { try { // rectangle.setSize(Toolkit.getDefaultToolkit().getScreenSize()); screenShot = robot.createScreenCapture(new Rectangle((Toolkit.getDefaultToolkit().getScreenSize()))); //screenShot = robot.createScreenCapture(new Rectangle(800,600)); //screenShot.se ImageIO.write(screenShot, "png", file); imageIcon=new ImageIcon(""+file); //imageIcon.setImage(screenShot); oos.writeObject(imageIcon); oos.flush(); //sleep(1000); screenShot = null; Runtime runtime= Runtime.getRuntime(); runtime.gc(); } catch (IOException ex) { ex.printStackTrace(); } //catch (InterruptedException ex) { // ex.printStackTrace(); // } } } } }
Thanks
Ads