0

Possible Duplicate:
StreamCorruptedException: invalid type code: AC

Hello guys I am trying to read object from the stream over the network... I am using ReadInputObject method ... and I am implementing it inside a thread to recieve the data ... here is the while loop in the thread

while((dp = (DataPackage) ois.readObject()) != null)
        {

             Ball b = new Ball();
             b.setX(dp.x);
             b.setY(dp.y);
             jTextField1.setText(b.getX() + "    " +b.getY());
             b.paint(ob.getGraphics());
             b.setVisible(true);
             ois.reset();

        }
    } catch (IOException ex) {
        System.out.println("Error 1 is Here");
    } catch (ClassNotFoundException ex) {
        System.out.println("Error 2 is here");
    }  

the problem is that the ois gets the first object and then .... it goes inside the exception and prints out this Error 1 is here

here is the code where I send the object

DataPackage dp = new DataPackage();
    dp.x=b1.getX();
    dp.y=b1.getY();
    dp.t=b1.getT();
    dp.dx=(int)b1.getDx();
    dp.dy=(int)b1.getDy();

    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(Server_login.client.getOutputStream());
    } catch (IOException ex) {
        Logger.getLogger(Game_Painters_right.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        oos.writeObject(dp);
    } catch (IOException ex) {
        Logger.getLogger(Game_Painters_right.class.getName()).log(Level.SEVERE, null, ex);
    }

what is the wrong in my code ?? thank you

Community
  • 1
  • 1
I.el-sayed
  • 325
  • 1
  • 5
  • 18
  • 1
    Print the StackTrace and add it to your question. Use `ex.printStackTrace()` in your `catch`-blocks. – Lukas Knuth Dec 08 '12 at 14:17
  • java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:347) at netbeans_gui_swing.Game_Painters_left$5.run(Game_Painters_left.java:221) – I.el-sayed Dec 08 '12 at 14:19
  • Put the **full** StackTrace into your question... – Lukas Knuth Dec 08 '12 at 14:28
  • This is the full stack dude ..... after I deleted ois.reset(); java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) at netbeans_gui_swing.Client_login$2.run(Client_login.java:98) – I.el-sayed Dec 08 '12 at 14:35
  • Checking for null is pointless unless you are planning to write a null to the stream. It doesn't return null at the end of the stream: it throws EOFException. – user207421 Dec 08 '12 at 23:48

1 Answers1

1

Well, remove the call to ois.reset(). Why is it there in the first place?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Before I didn't use ois.reset() but I wanted to clean the stream and get a new data in it .. that's why I used .reset()... Anyways I deleted it for u and here is the stack trace java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) at netbeans_gui_swing.Game_Painters_left$5.run(Game_Painters_left.java:212) – I.el-sayed Dec 08 '12 at 14:25
  • Then show us the code which sends the DataPackage instances to an ObjectOutputStream at the other side. reset() doesn't do what you think it does. Read the javadoc: http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#reset%28%29 – JB Nizet Dec 08 '12 at 14:27
  • I edited the code ... check it out .. included the part where I send the object – I.el-sayed Dec 08 '12 at 14:34
  • You're trying to read several objects at client side, but you only send one at server-side. You must read exactly the number of objects that have been sent. Trying to read more will lead to an IOException. – JB Nizet Dec 08 '12 at 14:42
  • Yes I know ... but the readObject should block whenever there is no input to read ..... !!! – I.el-sayed Dec 08 '12 at 14:44
  • What does the server do after the object is written in the output stream? – JB Nizet Dec 08 '12 at 14:51
  • nothing it just updates the position of the ball again when I click to the button update and send the new positions as an object again – I.el-sayed Dec 08 '12 at 14:58
  • 1
    So, in fact, it sends multiple DataPackage instance, but each time it sends one, it creates a new ObjectOutputStream, right? If so, don't do that? Use the same ObjectOutputStream to send all your objects, just as you're using the same ObjectInputStream to read them all. – JB Nizet Dec 08 '12 at 15:05
  • I did it but I still get he same exact problem . I initialised the oos in other place and every time I use it to send the datapackage ... but still not working .. the exact same stack trace ... – I.el-sayed Dec 08 '12 at 15:14
  • dude it is working .. yes u were correct – I.el-sayed Dec 08 '12 at 15:17