Java Remote Desktop(not working with Internet)
<p>My application has a server &amp; client,these 2 r connected through socket.Now the server will </p>

<p>control the desktop of client..client will capture the screens (by awt.Robot) and send it to </p>

<p>the server when ever an event occurs(mouse event or key event)..This is working fine &amp; I am </p>

<p>able to get the screen shots within no time through LAN &amp; WAN.But when I am connecting the </p>

<p>server &amp; client through Internet,the transfer a screen shots getting delayed,if an event </p>

<p>happens then the updated screen will come after 2 mins..I am storing the screen capture to </p>

<p>BufferedImage &amp; converting it to ImageIcon since it's serializable,&amp;  writing it to </p>

<p>ObjectOutputStream so that Server will catch the image through ObjectInputStream..</p>

<p>So How can I increase the speed of my app through Internet,I am not concerned about the </p>

<p>image quality..I just need fast transfer...</p>

<p>This is for Client...</p>

<pre><code>    import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.ImageIcon;

/**
 * This class is responisble for sending sreenshot every predefined duration
 */
class ScreenSpyer extends Thread {

    Socket socket = null; 
    Robot robot = null; // Used to capture screen
    Rectangle rectangle = null; //Used to represent screen dimensions
    boolean continueLoop = true; //Used to exit the program

    public ScreenSpyer(Socket socket, Robot robot,Rectangle rect) {
        this.socket = socket;
        this.robot = robot;
        rectangle = rect;
        start();
    }

    public void run(){
        ObjectOutputStream oos = null; //Used to write an object to the streem


        try{
            //Prepare ObjectOutputStream
            oos = new ObjectOutputStream(socket.getOutputStream());
            /*
             * Send screen size to the server in order to calculate correct mouse
             * location on the server's panel
             */
            oos.writeObject(rectangle);
        }catch(IOException ex){
            ex.printStackTrace();
        }

       while(continueLoop){
            //Capture screen
            BufferedImage image = robot.createScreenCapture(rectangle);
            /* I have to wrap BufferedImage with ImageIcon because BufferedImage class
             * does not implement Serializable interface
             */
            ImageIcon imageIcon = new ImageIcon(image);

            //Send captured screen to the server
            try {
                System.out.println("before sending image");

                oos.writeObject(imageIcon);
                oos.reset(); //Clear ObjectOutputStream cache
                System.out.println("New screenshot sent");
            } catch (IOException ex) {
               ex.printStackTrace();
            }

            //wait for 100ms to reduce network traffic
            try{
                Thread.sleep(100);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }

    }

}
</code></pre>

<p>This is for Server</p>

<pre><code>import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * @author Halim
 * ClientScreenReciever is responsible for recieving client screenshot and displaying
 * it in the server. Each connected client has a separate object of this class
 */
class ClientScreenReciever extends Thread {

    private ObjectInputStream cObjectInputStream = null;
    private JPanel cPanel = null;
    private boolean continueLoop = true;

    public ClientScreenReciever(ObjectInputStream ois, JPanel p) {
        cObjectInputStream = ois;
        cPanel = p;
        //start the thread and thus call the run method
        start();
    }

    public void run(){

            try {

                //Read screenshots of the client then draw them
                while(continueLoop){
                    //Recieve client screenshot and resize it to the current panel size
                    ImageIcon imageIcon = (ImageIcon) cObjectInputStream.readObject();
                    System.out.println("New image recieved");
                    Image image = imageIcon.getImage();
                    image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight()
                                                        ,Image.SCALE_FAST);
                    //Draw the recieved screenshot
                    Graphics graphics = cPanel.getGraphics();
                    graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
          } catch(ClassNotFoundException ex){
              ex.printStackTrace();
          }
     }
}
</code></pre>
