2

I m working on a java app which is based on sockets.It fetches the screen of the client and shows it in the GUI on the server.But the problem is it just shows the screen of the client at the time program was started and dont change it. Here is my code Server Side:

try {
    img = ImageIO.read(socket.getInputStream());

    while(true){
        ImageIcon icon = new ImageIcon(img);
        label.setIcon(icon);
    }
}
catch (IOException e) {}

Client Side:

 public class Client{

  public static void main(String[] args) throws Exception{

 BufferedImage screenShot = new Robot().createScreenCapture(new         
 Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
 ImageIO.write(screenShot, "PNG", socket.getOutputStream());
  public static void main(String[] args) throws Exception{
     Socket socket = new Socket("localhost",1999);
     Chat chat = new Chat(socket);      
     Thread thread = new Thread(chat);
     thread.start();
 }      
}


 class Chat implements Runnable{
 private Socket socket;

public Chat(Socket socket){
    this.socket = socket;

}

@Override
public void run() {
    // TODO Auto-generated method stub
    try{ while(true){
    BufferedImage screenShot = new Robot().createScreenCapture(new   Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(screenShot, "PNG", socket.getOutputStream());
    }}catch(Exception e){}


}}

ERROR:-
Exception in thread "Thread-3" java.lang.IndexOutOfBoundsException
at javax.imageio.stream.FileCacheImageOutputStream.seek(Unknown Source)
at javax.imageio.stream.FileCacheImageOutputStream.close(Unknown Source)
at com.sun.imageio.stream.StreamCloser$CloseAction.performAction(Unknown Source)
at com.sun.imageio.stream.StreamCloser$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
UPDATE:
Actually i was closing the socket even before connection was establishing that was causing the error. Else Nick's code just Worked fine.

Cybershadow
  • 1,097
  • 9
  • 16
  • you just read and write the image once? do it in an loop! (move at server the imageio.read in an loop, at clientside the complete screencapture functionality!). Maybe in an Timer with 5 seconds – Danny. Feb 12 '14 at 12:43
  • U mean i shud add While(true) loop in the client side file? – Cybershadow Feb 12 '14 at 12:45
  • yes otherwise you only upload one screenshot because the code is executed only one time. at server you have to read the socket more often (create a timer with 5 seconds or so...). – Danny. Feb 12 '14 at 12:46
  • OK i tried adding loop in client side. But still its not changing. and I dint get the timer part? – Cybershadow Feb 12 '14 at 12:48
  • You also to read it in an loop at server side. I just mentioned if it's not better to only send an image every 5 seconds instead of flooding it. If you don't implement an restrictions it will generate a lot of traffic. (Maybe have a look at client-server architectures in common, to understand how it's working and how to get the best way of sending-receiving). – Danny. Feb 12 '14 at 12:53
  • ok one more thing, how can i use timer(TimerTask,Long) to wait 5 secs? how can i convert 5 secs to Long? – Cybershadow Feb 12 '14 at 12:59
  • timers are working with milliseconds so long fiveSec = 5*1000 (Maybe have a look at the documentation?!) – Danny. Feb 12 '14 at 13:03
  • Now I m facing a new exception, i Updated the question , Any idea what could cause that? – Cybershadow Feb 12 '14 at 13:28
  • look at Server.java line 88 and backdoor.java line 74 (If you code, what I think I gonna skip this problem!) – Danny. Feb 12 '14 at 13:30
  • I beg ur pardon? skip this problem? – Cybershadow Feb 12 '14 at 13:38
  • Client side problem no longer exists now.Now i just get NullpointerException error in server side.What could be causing it?And in case u were saying to ignore this error , then still the Image is not changing – Cybershadow Feb 12 '14 at 14:09

1 Answers1

1

Because it's unclear, I want to make sure that you have spawned a new thread that the ImageIO.read call is running on; This line will likely block the thread until something is sent for it to read. You do not want this executing on the EDT.

Assuming you have that, I would suggest that you use SwingUtilities.invokeLater for updating the GUI. This is standard process - what it does is it puts the update in the queue, so the next time the GUI wants to update, it knows what to do.

So altogether, your code should look something like this:

Thread awesomeThread = new Thread(new Runnable(){

    @Override
    public void run() {
        while(true){

            try{
                //Read the image
                final Image img = ImageIO.read(socket.getInputStream());
                System.out.println("Image Read");  //code for troubleshooting

                //Once an image is read, notify the GUI to update
                SwingUtilities.invokeLater(new Runnable(){
                    @Override
                    public void run() {
                        ImageIcon icon = new ImageIcon(img);
                        label.setIcon(icon);
                        System.out.println("Image updated"); //code for troubleshooting
                    }});
            } catch (IOException e) {}
        }
    }});

   awesomeThread.start();
Nick Rippe
  • 6,465
  • 14
  • 30
  • Ok, now i m getting a new Exception, java.lang.IndexOutOfBoundsException in the ImageIo.write Line in the client side file.What might be causing that? – Cybershadow Feb 12 '14 at 16:07
  • I can't see any code in your sample that has to do with indices so I suspect you need to post more code. But it would most likely have to do with an array or an `ArrayList`. Does the stack trace reference your code at any point? That should give you the line that's causing problems. – Nick Rippe Feb 12 '14 at 16:11
  • Actually stack is giving no line.it just says Exception in thread-3 – Cybershadow Feb 12 '14 at 16:13
  • I just included full code of client side , and also the error i m getting in stack. What might be causing it?? – Cybershadow Feb 12 '14 at 16:20
  • I suspect it's coming from the `run` method. I'd suggest printing a stack trace there (if you're not already). Your code is pretty jumbled up - you have nested `main` methods in the `Client` class. I think there must have been a copy/paste error. – Nick Rippe Feb 12 '14 at 16:31
  • Actuallly i found what was causing the error.I closed socket in server side even before connection was establishing and your code just worked fine! – Cybershadow Feb 12 '14 at 16:33