Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.StreamCorruptedException: invalid stream header: 7371007E

I have a client Server application which communicate using objects.
when I send only one object from the client to server all works well.
when I attempt to send several objects one after another on the same stream I get

StreamCorruptedException.  

Can some one direct me to the cause of this error?

client write method

   private SecMessage[] send(SecMessage[] msgs) 
   {
     SecMessage result[]=new SecMessage[msgs.length];
      Socket s=null;
      ObjectOutputStream objOut =null;
      ObjectInputStream objIn=null;
      try
      {
       s=new Socket("localhost",12345);
       objOut=new ObjectOutputStream( s.getOutputStream());
       for (SecMessage msg : msgs) 
       {
            objOut.writeObject(msg);
       }
       objOut.flush();
       objIn=new ObjectInputStream(s.getInputStream());
       for (int i=0;i<result.length;i++)
            result[i]=(SecMessage)objIn.readObject();
      }
      catch(java.io.IOException e)
      {
       alert(IO_ERROR_MSG+"\n"+e.getMessage());
      } 
      catch (ClassNotFoundException e) 
      {
       alert(INTERNAL_ERROR+"\n"+e.getMessage());
      }
      finally
      {
       try {objIn.close();} catch (IOException e) {}
       try {objOut.close();} catch (IOException e) {}
      }
      return result;
 }

server read method

//in is an inputStream Defined in the server
SecMessage rcvdMsgObj;
rcvdMsgObj=(SecMessage)new ObjectInputStream(in).readObject();
return rcvdMsgObj;

and the SecMessage Class is

public class SecMessage implements java.io.Serializable
{
 private static final long serialVersionUID = 3940341617988134707L;
 private String cmd;
    //... nothing interesting here , just a bunch of fields , getter and setters
}
like image 412
Alex Avatar asked May 30 '10 14:05

Alex


2 Answers

If you are sending multiple objects, it's often simplest to put them some kind of holder/collection like an Object[] or List. It saves you having to explicitly check for end of stream and takes care of transmitting explicitly how many objects are in the stream.

EDIT: Now that I formatted the code, I see you already have the messages in an array. Simply write the array to the object stream, and read the array on the server side.

Your "server read method" is only reading one object. If it is called multiple times, you will get an error since it is trying to open several object streams from the same input stream. This will not work, since all objects were written to the same object stream on the client side, so you have to mirror this arrangement on the server side. That is, use one object input stream and read multiple objects from that.

(The error you get is because the objectOutputStream writes a header, which is expected by objectIutputStream. As you are not writing multiple streams, but simply multiple objects, then the next objectInputStream created on the socket input fails to find a second header, and throws an exception.)

To fix it, create the objectInputStream when you accept the socket connection. Pass this objectInputStream to your server read method and read Object from that.

like image 50
mdma Avatar answered Nov 05 '22 13:11

mdma


when I send only one object from the client to server all works well.

when I attempt to send several objects one after another on the same stream I get StreamCorruptedException.

Actually, your client code is writing one object to the server and reading multiple objects from the server. And there is nothing on the server side that is writing the objects that the client is trying to read.

like image 2
Stephen C Avatar answered Nov 05 '22 11:11

Stephen C