Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    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
        }
    }
    Last edited by John20001; March 2nd, 2012 at 04:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Where is the definition for the TheImage class and ClientFrame?

    You have some catch blocks without calls to printStackTrace(). You could be missing some important exceptions.
    Last edited by Norm; March 2nd, 2012 at 08:13 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Hello, Norm, thanks so much for replying! As I said, TheImage is just a wrapper for a BufferedImage (Since it isnt serializable), and ClientFrame is a simple GUI that just prints the received image. Essencially the end product will allow the client to click on this frame, hence sending the click instruction for that location to the server, the server executing that instruction, then sending the updated screen back to the client. This will make it feel like the ClientFrame is a "window" into the servers computer. Since clicking the ClientFrame will actually do things realtime. Of course now, im just doing the viewing part, none of the clicking/etc. I just need to get past this bizarre crash. Thanks for the printStackTrace() tip, ill do that. Here is the code, though:

    TheImage.java:

    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
     
    public class TheImage implements Serializable
    {
        public BufferedImage bi;
     
        public TheImage()
        {
     
        }
    }

    And ClientFrame.java:

    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
     
    public class ClientFrame extends javax.swing.JFrame {
     
        private Rectangle frame;
        private BufferedImage bi;
     
        public ClientFrame(int resX, int resY) 
        {
            initComponents();
            frame = new Rectangle(0, 0, resX, resY);
            this.setSize(resX, resY);
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        /**
         * @param args the command line arguments
         */
        public void start() 
        {
           this.setVisible(true);
        }
     
        public int getMouseX()
        {
            if(this.getMousePosition() != null)
            {
               return this.getMousePosition().x;
            }
            return 0;
        }
     
        public int getMouseY()
        {
            if(this.getMousePosition() != null)
            {
               return this.getMousePosition().y;
            }
            return 0;
        }
     
     
     
        public void updateImage(BufferedImage bi)
        {
            this.bi = bi;
            repaint();
        }
     
        @Override
        public void paint(Graphics g)
        {
            g.clearRect(frame.x, frame.y, frame.width, frame.height);
            g.drawImage(bi, 0,0, this);
     
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        // End of variables declaration//GEN-END:variables
    }

    Thanks again!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Do you have a driver program for testing? I don't see any main() methods for executing these classes.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Ah, forgive me, I've been quite busy the past few days. Yes, its quite simple, which is why i'm so confused!

    import java.util.Scanner;
    public class Main 
    {
        public static void main(String args[])
        {
           ClientFrame cf;
           String ip;
           Scanner scan;
           int clse;
           int x;
           int y;
     
           scan = new Scanner(System.in);
     
           System.out.println("Enter ip address.");
           ip = scan.nextLine();
           System.out.println("1. client, 2. server");
           clse = scan.nextInt();
           System.out.println("Resolution? X then Y");
           x = scan.nextInt();
           y = scan.nextInt();
     
           if(clse == 1)
           {
               Client c = new Client(ip, x, y);
               c.start();
           }
           else
           {
               Server s = new Server(ip, x, y);
               s.start();
           }
     
     
        }
     
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Can you post the console from when you execute the program.
    Here is what I get when I run Server and Client in the same JVM:

    Running: F:\Java\jre6\bin\java.exe -classpath D:\JavaDevelopment;.;..\. -Xmx512M ClientServer_3

    CF constr
    C Before...connected!
    S Connected!
    S comm
    C comm
    S sending Serving
    S respLen=14
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=0, cntr=0
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=1, cntr=1
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=2, cntr=2
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=3, cntr=3
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=4, cntr=4
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=5, cntr=5
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=6, cntr=6
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=7, cntr=7
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=8, cntr=8
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=9, cntr=9
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=10, cntr=10
    C resp.Len=20<
    C reading image bytes avail=0
    S sending image
    C sending got it
    S inst=got it cntr=11, cntr=11
    C resp.Len=20<
    C reading image bytes avail=0

    0 error(s)


    The program seems to run until I stop it.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Sure, and thanks again for spending time to help me! I put a few print statements in critical regions (which of course drastically reduced my clientframe framerate). I noticed something interesting. Something is clearly going on with the communication of the server and client. I put a counter in the communication loops (in communicate()) and printed it out at each loop. Everything was fine until the clientframe went grey. Not exceptions, no nothing, except that the server console stopped (meaning stuck on a read I think), and the client console went out of control. The counter jumped from 150 to the thousands... after about 4000 it stopped too. No exceptions. The output would be gargantuan, so ill just post the endings (theyre just the same statements repeated over and over).

    This is done in netbeans, so i can easily run server and client side by side...

    Server end output:

    Next iteration from server...151
    Sent the image...
    Next iteration from server...152
    Sent the image...
    Next iteration from server...153
    Sent the image...
    Next iteration from server...154
    Sent the image...
    Next iteration from server...155
    Sent the image...
    Next iteration from server...156
    Sent the image...
    Next iteration from server...157

    Client middle output (Around 157 where server stops):

    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...154
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    cleared
    Starting next iteration in client...155
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...156
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...157
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...158
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...159
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    cleared
    Drawn
    Starting next iteration in client...160
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...161
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Starting next iteration in client...162
    Painting...
    cleared
    Drawn

    Then client will go on until about 4287 iterations... then it just freezes. Before "Starting next iteration in client" which is right below a readline(). So its stuck in a readline. Its as if its successfully reading from ImageIO.read all those iterations, but server isnt writing. Is something out of sync in the threads (only threads are server and client)? I tried synchronized(this) but it didnt seem to help. This is very odd, since the communication seems to... continue... even though client has readlines and I thought they were blocking until server wrote... I wish I could send you a video of me running it. But hopefully this will give some insight on a potential problem?

  8. The Following User Says Thank You to John20001 For This Useful Post:

    Norm (March 6th, 2012)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    What I haven't figured out yet is shown in this from my debug output posted earlier:
    C resp.Len=20<
    The response that is read is always 20 characters long but the data that was sent was less than 10.
    That is read in this statement in the communicate() method:
    String resp = br.readLine();
    System.out.println("C resp.Len=" + resp.length() + "<");

    Have you checked that the Strings sent as messages are read properly?

  10. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Well, I printed out what the string was... And this is where I begin to get cranky with server/client programming. Inconsistencies. I think you may verify that my server and client PrintWriter and BufferedReader code instantiations are identical? Indeed. So on the server side, I print out the message client sends: "got it". Server recieves this message just fine, and is 100% correct. However, when server sends "done" to client, client gets... well... heres a few examples:

    Ahh, nevermind, apparently copy and pasting what it gives me are some sort of commands to this forum, or merely unprintable characters. Let [box] mean a literal ASCII square. The printout was then:

    \[box]>[box] IEND[box]B'done
    [box] [box] IEND[box]B'done
    [box]R_hat[box] IEND[box]B'done

    As you can see, the endings are always quite the same. These results should be consistent with the code posted above (the code you seem to have executed). So... it appears to be some representation for the ending of a stream, some garbage and then done just appended to the end. I have a feeling that this is what is eventually breaking the program...

    After the program breaks, it now recieves this garbage (again, server is now stuck in a read, client is just somehow burning thru its loop, and its reads are giving the following) :
    IEND[box]B'[box]NG
    IHDR [box] [box] D[box]H[box]g[box][box]w[box] [box]f4N3[box][box]

    and so on, for about 15 more characters. It just completely breaks. Is my code doing anything particularly fancy here? It should be an extremely straightforward "recieve reply receive reply" model.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    What your code does that is different is to call getOutputStream() and wrap it in different classes each of which uses the stream differently. This may confuse what is being written to the streams. I'd suggest that you only get the output stream one time and use that one outstream for all the writing and now mix its usage.
    Or have two connections: one for data and one for communication protocol messages.

  12. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Well, my friend, you are very correct. I stopped the ImageIO reads and writes on the streams with comments, and the messages (comm prot) are sent wonderfully! So writing different types of data on the same output stream is very very bad... So I must ask your recommendation. Two connections seems viable, since there would be 2 output streams and 2 input streams. Perfect! However, this seems wasteful? (Maybe its not, im a networking novice). Is there any other way? I did only invoke the get#Stream() methods once, and store them in variables. That still didnt work. It was the writing of different things (or using the streams with 2 different classes) that confused it. I guess im asking is this what people normally do when they want to send not only messages? Or would you use only an Object Sender class like ObjectOutputStream and ObjectInputStream and send Strings for messages? I think that sounds like it would work too? Then I could send my TheImage class which holds a serialized BufferedImage inside.
    Last edited by John20001; March 7th, 2012 at 03:39 PM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    I think it would be possible to send different types of data on the same stream. You would use the same class to control all the I/O and not two different wrapper classes like you were doing.
    For example if you only sent bytes on the stream then you'd need to provide front ends to convert the messages/data to bytes and back on the other end.

  14. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Yes, im now using ObjectOutputStream and it seems to work okay, however not for the images... gives the exception message java.awt.image.BufferedImage. I guess that means even though i put the buffered image class in a wrapper serialized class, I cant do that. Ill have to figure out how to change the BufferedImage into bytes or some format that CAN be sent over a socket, and then convert it back to an image on the other side. Thanks so much for your help, I've learned that when dealing with i/o streams, you should only use one type of class for a given stream. Or, of course, just use the outputstreams write bytes and the input streams read bytes.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    ImageIO write() will write to an OutputStream. There is a ByteArrayOutputStream class that could capture the bytes.

  16. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Ahh yes, that does work, however if used in conjunction with ObjectOutputStream (for comm prot) it crashes the program for some reason or another. I think if I just use ImageIO write() and the output streams write(bytes[]) and the input streams read(bytes[]) it should work. Its all just writing and reading bytes, so should work...

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    it crashes the program
    What is the error message? And what class/method causes it?

  18. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Well, these are the modified methods:

    Server.java - Communicate()
    public void communicate()
        {
            int counter = 0;
            String instruction;
     
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException ie)
            {
     
            }
            try
            {
                oos.reset();
                oos.writeObject(new String("Lets go!"));
               System.out.println( (String)ois.readObject());
            }
            catch(IOException ioe)
            {
     
            }
            catch(ClassNotFoundException cnfe)
            {
     
            }
            while(true)
            { 
                try
                { 
     
                   while((ti.bi = rob.createScreenCapture(frame)) == null);
                   ImageIO.write(ti.bi, "PNG", out);
                   System.out.println("Sent the image...");
                   instruction = (String)ois.readObject();
                   System.out.println("Instruction was got it?: " + instruction);
                   handleCommand(instruction); // Not yet implemented
                   oos.reset();
                   oos.writeObject(new String("done"));
                   System.out.println("Next iteration from server..." + counter++);
     
     
     
                }
                catch(IOException ioe)
                {
                    System.out.println("IO exception in server :(");
                    System.out.println(ioe.getMessage());
                    ioe.printStackTrace();
                }
                catch(ClassNotFoundException cnfe)
                {
     
                }
            }
        }

    Client.java - Communicate()

    public void communicate()
        {
            int counter = 0;
            String temp;
            TheImage temp2;
            try
            {
               System.out.println( (String)ois.readObject());
               oos.reset();
               oos.writeObject(new String("Okay lets go!"));
            }
            catch(IOException ioe)
            {
     
            }
            catch(ClassNotFoundException cnfe)
            {
     
            }
               while(true)
               { 
                    try
                    {
                       curImage = ImageIO.read(in);
                       //temp2 = (TheImage)ois.readObject();
                      // curImage = temp2.bi;
     
                       System.out.println("Got the image in client!");
                       cf.updateImage(curImage);
                       oos.reset();
                       oos.writeObject(new String("got it!"));
                       temp = (String)ois.readObject();
                       System.out.println("Is done? : " + temp);
                       System.out.println("Starting next iteration in client..." + counter++);
     
                    }
                    catch(IOException ioe)
                    {
                        System.out.println("Io exception in client :(");
                        System.out.println(ioe.getMessage());
                        ioe.printStackTrace();
                    }
                    catch(ClassNotFoundException cnfe)
                    {
     
                    }
               }
        }

    The resets basically disregard objects in the streams. Without them, it still crashes (was just trying to figure out what was going on). The "crash" is detailed in the client output, and is as follows:

    Lets go!
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    cleared
    Io exception in client
    invalid type code: 3B
    java.io.StreamCorruptedException: invalid type code: 3B
    Got the image in client!
    Updating image in clientFrame!
    Io exception in client
    invalid type code: 3B
    Drawn
    Painting...
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    Io exception in client
    invalid type code: 3B
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    Io exception in client
    invalid type code: 3B
    cleared
    Drawn
    Got the image in client!
    Updating image in clientFrame!
    Painting...
    Io exception in client
    invalid type code: 3B
    cleared
    Drawn
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1374)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:369)
    at Client.communicate(Client.java:82)
    at Client.run(Client.java:49)

    Perhaps my message was unclear though. I meant that though this didnt work, perhaps just using write(bytes[]), read(bytes[]), and ImageIO.read and write would work, since you had mentioned that ImageIO.write is a class representing a byte array? ByteArrayOutputStream

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    you had mentioned that ImageIO.write is a class representing a byte array?
    Don't know where that idea came from. What I meant was: The write method can write the bytes of an image to a byte array.

  20. #19
    Junior Member
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Ohhh!! Im sorry I misunderstood. Instead of using ImageIO to write to the socket output stream, I could write to a ByteArrayOutputStream and capture the bytes. Then send these bytes over the socket. I see! I will try this, thank you!

  21. #20
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Simple Remote control program - ImageIO exceptions , no idea why! Help please :(

    Unpredictable freezes are a good sign of a memory problem, any chance you are keeping track of/creating objects you don't need to be? something simple like making a new file reader inside a loop could be eating up tons of memory. A good way to test this with out having to debug is run your program with 10-20 other applications running before you open/run it and see if your average problem spot of 4287 iterations shrinks noticeably.

Similar Threads

  1. [SOLVED] Control remote computer
    By phabion in forum Java Networking
    Replies: 3
    Last Post: March 13th, 2012, 09:19 PM
  2. A Program Idea
    By drbacchoi in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2012, 10:15 AM
  3. Help me...its urgent...its simple but no idea
    By zurich91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2012, 01:50 PM
  4. How to make a simple and efficient web control panel.
    By kelsmith11 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 18th, 2011, 02:28 PM
  5. Replies: 0
    Last Post: December 12th, 2009, 10:09 PM