Problem sending serializable objects using ObjectInputStream

server code snippet:

 public void run() {
        try {
          // Create data input and output streams
          ObjectInputStream inputFromClient = new ObjectInputStream(
            socket.getInputStream());
          ObjectOutputStream outputToClient = new ObjectOutputStream(
            socket.getOutputStream());

          while (true) {

         cop = inputFromClient.readObject();

         String[][] m1=new String[][] {{"1", "1","1"}};
         Object xx=new getSerialModel(m1);
         outputToClient.reset();
         outputToClient.writeObject(xx);

         outputToClient.flush();


          }
        }

from customer:

//////////////
    /// sockt jop
    try {
    // Create a socket to connect to the server
   socket = new Socket("127.0.0."+Math.round(50+Math.random()*50), 8000);

    // Create an output stream to send data to the server
   toServer = new ObjectOutputStream(socket.getOutputStream());
   toServer.flush();

  }
  catch (IOException ex) {
    msgArea.append('\n' + ex.toString() + '\n');
  }
///////////////////
//***
///////////////////
buttonSave.addActionListener(new ActionListener()

{ public void actionPerformed(ActionEvent ev)

{

System.out.println("Saving data is not implemented yet.");
        String[][] m1={{"0","0","0"}};
        for ( int i = 0 ; i < tableModel.getRowCount() ; i++ ){
            { for ( int j = 0 ; j < tableModel.getColumnCount() ; j++ )
                    m1[i][j]=(String)tableModel.getValueAt(i, j) ;
            }
        }

        getSerialModel obt =new getSerialModel(m1);

        try{
            toServer.reset();
        toServer.writeObject(obt);
       toServer.flush();


        }
        catch (Exception ex) {
     msgArea.append("cant reach the server its may be off" + '\n');
   }

}

});
// button send msg
    buttonsendtest.addActionListener(new ActionListener()

{ public void actionPerformed(ActionEvent ev)

{
        try{


       fromServer = new ObjectInputStream(socket.getInputStream());

       Object mdata = fromServer.readObject();
       tableModel.setDataVector((((getSerialModel)mdata).getmodel()), columnNames);
       table.updateUI();

        }
        catch (Exception ex) {
            System.out.print(ex.getStackTrace());
     msgArea.append("cant reach the server its may be off "+ ex.toString() + '\n');
   }

}
});

When I try to read a serializable object from multiple servers once, I get this exception, the first time the receiver reads it successfully.

java.io.StreamCorruptedException: invalid stream header: 00007571

how can i fix this?

+3
source share
4 answers

ObjectInputStream , . , , , , ObjectInputStream . ObjectInputStream ObjectOutputStream , , .

, ObjectOutputStream (.. writeObject()), - ( ),

, , , . ObjectInputStream - - , ( readObject()); , / .. readObject(), .

, - :

objectOut.reset()
objectOut.writeObject(foo);

reset() , , . , , ObjectOutputStream . , .

+7

ObjectInputStream.readObject(), :

ObjectInputStream. , , . writeObject readObject. readObject.

, , , . , . ( readObject ), .

InputStream . InputStream ; , .

Specified by:
    readObject in interface ObjectInput

Returns:
    the object read from the stream 
Throws:
    ClassNotFoundException - Class of a serialized object cannot be found. 
    InvalidClassException - Something is wrong with a class used by serialization. 
    StreamCorruptedException - Control information in the stream is inconsistent. 
    OptionalDataException - Primitive data was found in the stream instead of objects. 
    IOException - Any of the usual Input/Output related exceptions.

, , , , .

0

"". ? , , , serialVersionUID, ? , , . , UID .

0

Perhaps you are trying to read the same object from the stream several times, while the server wrote the object only once.

Or you try to use an ObjectInputStream before creating the corresponding ObjectOutputStream and this invalidates the relationship between them. An ObjectOutputStream writes the header of the serialization stream when it is created, and if it was not created before the corresponding ObjectOutputStream, this header will be lost.

-1
source

Source: https://habr.com/ru/post/1751048/


All Articles