J.Olufsen
J.Olufsen

Reputation: 13915

StreamCorruptedException when trying to pass InputStream

Getting error when trying to pass socket InputStream variable to thread to write object. I have client and two thread: first reading text input, second reads serialized objects. They run simultaneusly, therefore I set volatile static InputStream socketIs; . Is that right? How to fix it?

java.io.StreamCorruptedException: invalid stream header: 57656C63
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at com.client.ClientThreadInObj.<init>(ClientThreadInObj.java:25)
    at com.client.Client.main(Client.java:77)

    public class Client { 
        static ClientThreadIn threadIn;
        static ClientThreadInObj threadInObj;
        volatile static InputStream socketIs; 
        volatile static PrintStream ps;
        volatile static BufferedReader in; 
        static PrintWriter out;
        static Socket s;

    public static void main(String[] args){
             in = null; 
             out = null; 
             socketIs =null;
             threadIn = null;
             threadInObj = null;
             try{ 
                socketIs = s.getInputStream();
                in = new BufferedReader(new InputStreamReader(socketIs)); 
                out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                JPanelOne gui= startGUI();
                gui.setOutputPrintWriter(out);
                OutputStream outStream = gui.getRefToOutputStream(); 
                ps =  new PrintStream(outStream); 

                threadIn = new ClientThreadIn(in, ps, gui);
                threadInObj = new ClientThreadInObj(socketIs, ps, gui); // ERROR

                threadIn.start();
                threadInObj.start();
            catch(IOException ioe) 
        { ..}
...
    }

Server:

...
Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            FlattenLot fL = new FlattenLot(clientThread.m_clientSocket.getOutputStream(), currentLot);
                            fL.sendObj();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }, 100);
...

Debug shows error on server side while trying to out.writeObject(lot); package serial;

import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;

import com.server.Lot;

public class FlattenLot {

    OutputStream oStream;
    Lot lot;

    public FlattenLot(OutputStream os, Lot l){
        lot = l;
        oStream = os;
    }

    public void sendObj(){
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(oStream);
            out.writeObject(lot); // ERROR: java.io.NotSerializableException
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

Lot:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import java.util.Date;
import java.util.Calendar;

public class Lot implements Serializable{
    private static final long serialVersionUID = 1L;
    private  String NAME;
    public synchronized String getName(){return NAME;}
}

Lot implements Serializable. Why was thrown java.io.NotSerializableException? I have got Lot object successfully from first thread which used BufferReader(Input stream) to get data. Second thread cannot read object probably because it was red by first thread. Seems like two threads race for socket input stream despite the fact that the InputStream is = socket.getInputStream() variable defined as volatile and have to assure thread safe collaboration....

Upvotes: 1

Views: 2268

Answers (1)

user207421
user207421

Reputation: 310860

Use a single ObjectInputStream and ObjectOutputStream for the life of the socket.

Upvotes: 1

Related Questions