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.