Hello all. This is my first post, and I do apologize for perhaps posting in the wrong place, structuring my code poorly, or otherwise. I just threw this together to essentially have a java Jframe window on the client side that can be viewed and clicked on to remotely control another computer (at their consent, of course). The image that fills this jframe is a buffered image screenshot (via Robot) sent thru ImageIO.write(). Right now, I only implement viewing the server-side computers desktop interactively. I do not have mouse moving/clicking code yet. This program works exactly as intended for anywhere between 20 seconds and 40 seconds, then it crashes with an exception on the server side.
Server: Exception in thread "Thread-0" java.lang.IndexOutOfBoundsException
at javax.imageio.stream.FileCacheImageOutputStream.se ek(FileCacheImageOutputStream.java:166)
at javax.imageio.stream.FileCacheImageOutputStream.cl ose(FileCacheImageOutputStream.java:227)
at javax.imageio.ImageIO.write(ImageIO.java:1580)
at Server.communicate(Server.java:82)
at Server.run(Server.java:39)
I'm not using an array, this exception is somewhere buried in the ImageIO.write code. If anyone has experience with sending images real-time over a server-client connection, I was hoping they could please tell me if im doing anything terrible wrong in my code? The basic overview is:
Server: Establish connection. Begin communication protocol (communicate method via run()). Once client is acknowledged, take screenshot (via Robot), send screenshot, wait to hear from client, repeat. Also will execute commands from client once implemented.
Client: Establish connection. Begin communication protocol (communicate method via run()). Once connection is solid, read the screenshot, update the image in the client frame, send any instructions (mouse moves etc. not yet implemented), then repeat.
Other classes: Main, ClientFrame, TheImage: Main just gets the ip address. ClientFrame just constantly paints the image to a Jframe. TheImage is a wrapper class for a BufferedImage, from when I was trying to serialize a BufferedImage.
Thanks so much!
Client.java:
import java.io.*; import java.net.*; import java.awt.*; import java.awt.image.*; import javax.imageio.ImageIO; public class Client extends Thread { private Socket cs; private PrintWriter pw; private BufferedReader br; private Rectangle frame; private BufferedImage curImage; private ClientFrame cf; public Client(String ip, int xRes, int yRes) { frame = new Rectangle(0, 0, xRes, yRes); cf = new ClientFrame(xRes, yRes); try { cs = new Socket(InetAddress.getByName(ip), 4444); pw = new PrintWriter(cs.getOutputStream(), true); br = new BufferedReader(new InputStreamReader(cs.getInputStream())); System.out.println("Before..."); System.out.println("connected!"); } catch(IOException ioe) { System.out.println("Problems in client."); } cf.start(); } public void run() { communicate(); } public void communicate() { try { br.readLine(); pw.println("Okay, lets go!"); } catch(IOException ioe) { } while(true) { try { curImage = ImageIO.read(cs.getInputStream()); cf.updateImage(curImage); pw.println("got it"); br.readLine(); } catch(IOException ioe) { System.out.println("Io exception :("); } } } }
Server.java:
import java.io.*; import java.net.*; import java.awt.*; import javax.imageio.ImageIO; public class Server extends Thread { private ServerSocket ss; private Socket cs; private PrintWriter pw; private BufferedReader br; private Robot rob; private TheImage ti; private Rectangle frame; public Server(String ip, int xRes, int yRes) { frame = new Rectangle(0, 0, xRes, yRes); try { rob = new Robot(); ti = new TheImage(); // TheImage is just a wrapper class with a BufferedImage inside. ss = new ServerSocket(4444, 10, InetAddress.getByName(ip)); } catch(IOException ioe) { System.out.println("Problems in server."); } catch(AWTException awte) { } connect(); } public void run() { communicate(); } public void connect() { try { cs = ss.accept(); pw = new PrintWriter(cs.getOutputStream(), true); br = new BufferedReader(new InputStreamReader(cs.getInputStream())); System.out.println("Connected!"); } catch(IOException ioe) { System.out.println("Problems in connect."); } } public void communicate() { String instruction; try { Thread.sleep(100); } catch(InterruptedException ie) { } try { pw.println("Serving..."); br.readLine(); } catch(IOException ioe) { } while(true) { try { while((ti.bi = rob.createScreenCapture(frame)) == null); ImageIO.write(ti.bi, "PNG", cs.getOutputStream()); instruction = br.readLine(); handleCommand(instruction); // Not yet implemented pw.println("done"); } catch(IOException ioe) { System.out.println("IO exception :("); System.out.println(ioe.getMessage()); } } } public void handleCommand(String instruction) { // Not yet implemented } }