java.io.StreamCorruptedException:

java.io.StreamCorruptedException: invalid stream header

My client sends a packet to the server on a DatagramSocket on port 9991. The server receives this packet on a DatagramSocket on port 9991 and sends a packet back on a DatagramSocket on port 9999. The client is then trying to receive this packet fromt he server on a MulticastSocket on port 9999 but i get the above exception at this line in the clients receive method

ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStream));

I cant find a cause for this on the net so im hoping somewhere here can help. Thanks.

come on, someone must have an idea!

Most people don’t use object streams, and if you get a simple error like that you probalby just need to do some debugging.

Whate you may need to do is download ethereal.com and look at the packets being tranferred. And, of course, double-check that you’re using the API properly.

well as far as i can make out from reading on the net, the error is something to do with an object input stream trying to read a header on a packet that was never created by an object output stream, or else its somethign to do with creating multiple object input/output streams. no matter what i try, i cant seem to fix it.
So what do people usually use for sending non-primative data types using UPD (i need to send an AffineTransform and 2 floats in each packet) if they dont use Object Streams?

An AffineTransform consist of 3x3 matrix of wich only 6 values are used. You can get the content by using getMatrix(double[]). Write the doubles to your stream using DataOutputStream. The opposite on the other end. DataInputStream to read the doubles, the AffineTransform constructor to create it.

I cant get this to work, so ill explain what im doing here so you can tell me if im going about this the wrong way.

Client: updateServer()

                  buf_in = new byte[6000];
                  buf_out = new byte[6000];            
                  
                  ByteArrayOutputStream byteStreamOut = new ByteArrayOutputStream(6000);
                  ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(byteStreamOut));
                System.out.println("Client is writing to OS...");
                
                os.flush();
                os.writeObject(trans_move);
                os.writeFloat(x);
                os.writeFloat(y);
                os.writeInt(world.getMyPlayerID());
                os.flush();
                
                System.out.println("Client is finished writing to OS...");

                buf_out = byteStreamOut.toByteArray();
                packet_out = new DatagramPacket(buf_out, buf_out.length, address, 9991);
                sock_out.send(packet_out);      
                os.close();

Client: receiveUpdate()

                        buf_in = new byte[6000];
                        buf_out = new byte[6000];
                        
                      packet_in = new DatagramPacket(buf_in, buf_in.length);
                      System.out.println("(receiveUpdate)Client is waiting to recieve...");
                      multi_socket.receive(packet_in);
                      
                      ByteArrayInputStream byteStreamIn = new ByteArrayInputStream(buf_in);
                      ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStreamIn));
                
                      System.out.println("(receiveUpdate)Client has recieved packet!");
                      AffineTransform transMove = (AffineTransform)is.readObject();
                      float x = (float)is.readFloat();
                      float y = (float)is.readFloat();
                      int playerID = (int)is.readInt();
                      
                      is.close();

The client sends to the server using updateServer(), the server then receives the packet and sends it out to all clients. The client receives the server packets using receiveUpdate().
Ive tried make the server receive and send using the same code as the client uses, and also tried make it simply receive the packet and send it straight back out (without using object input/output at all). I still get the

Exception:  java.io.StreamCorruptedException: invalid stream header
java.io.StreamCorruptedException: invalid stream header
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
      at java.io.ObjectInputStream.<init>(Unknown Source)
      at Client.receiveUpdate(Client.java:211)

in the clients receiveUpdate() at

ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStreamIn));

Ive spent about 3 weeks on this now and its got to the point where im seriously short on time (its a college project) so all help is appreciated. Thanks.

Are you sure of what you are receiving ?

I’m not fluent with datagrams but those packets are not guaranteed to reach their destinations, and can be received in any order.

You could dump the byte array content on send and on receive to see if parts are missing / mixed.

Lilian

That don’t mather much as the content of a packet guaranteed to be intact, and that is what is parsed.

It can be a bug on the server that corrupts the data. As c_lilian said, print what you send and what you recieved. If it is not the same it is a network problem and nothing to do with the object stream.

ive managed to fix it. And the problem was… there was no problem in the 1st place!!! :stuck_out_tongue: I simply ran the server in a seperate application to the client and it works fine. Thanks for your help anyway.

actually, its not fixed! I still get the same error at the same line, but both clients can see each others players, and can see each other moving. The problem seems to be with the AffineTransform cause the movement isnt right.
If client 1 moves his player up, client 2 seems him move left. If he moves down, client 2 sees him move right. If he moves up or down, client 2 doesnt see him move at all. Strange! ???

** UPDATE **
If i comment out the lines in the receive method that are trying to read data in from the ObjectInputStream i still get the same error at the same place, so the error is happening when i try create the ObjectInputStream, not when i try read in from it. I already created an object input stream in the same class earlier and that doesnt give an error so is there something wrong with creating a 2nd objectInputStream in the same class?

If you have more than one wrapper for the stream then obviously you have to be very careful that they dont each take the data the other was expecting.

Buffered wrapper round the stream? Could it be taking aggresively?

Sorry, i dont understand what you mean. Could you explain that a little more for me?