Hi, I've been trying to send a BufferedImage
through a socket.
I can get it to work once, but when I try to get another screenshot, it fails.
The server usually gives me a java.lang.IndexOutOfBoundsException
at ImageIO.write(capture, "png", client.getOutputStream());
Server Code:
//already told server i'm expecting an image
try {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
client.getOutputStream().flush();
ImageIO.write(capture, "png", client.getOutputStream());
client.getOutputStream().flush();
} catch (Exception e) {
out.println("Error getting screenshot");
log(e, "Error getting screenshot");
}
Client Code:
public void screenshot() {
try {
BufferedImage image = ImageIO.read(socket.getInputStream());
if(image != null) {
new ImageWindow(image); //creates a viewing frame for the image
} else {
System.out.println("Screenshot failed"); //2nd and 3rd.. etc. screenshot fails (only 1st works)
}
} catch (Exception e) {
e.printStackTrace();
}
}
So why does the image transfer work the first time, but fails the 2nd time? Something is wrong with sockets. Also, there probably is a better way to send the image, so just tell me if you have any ideas