[java] 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
}

This question is related to java serialization io

The answer is


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.


This exception may also occur if you are using Sockets on one side and SSLSockets on the other. Consistency is important.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to serialization

laravel Unable to prepare route ... for serialization. Uses Closure TypeError: Object of type 'bytes' is not JSON serializable Best way to save a trained model in PyTorch? Convert Dictionary to JSON in Swift Java: JSON -> Protobuf & back conversion Understanding passport serialize deserialize How to generate serial version UID in Intellij Parcelable encountered IOException writing serializable object getactivity() Task not serializable: java.io.NotSerializableException when calling function outside closure only on classes not objects Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly

Examples related to io

Reading file using relative path in python project How to write to a CSV line by line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder Exception: Unexpected end of ZLIB input stream How to get File Created Date and Modified Date Printing Mongo query output to a file while in the mongo shell Load data from txt with pandas Writing File to Temp Folder How to get resources directory path programmatically ValueError : I/O operation on closed file