尝试传递InputStream时StreamCorruptedException

时间:2012-06-06 01:57:24

标签: java multithreading sockets serialization

尝试将socket InputStream变量传递给线程以写入对象时出错。我有客户端和两个线程:第一次读取文本输入,第二次读取序列化对象。它们同时运行,因此我设置了 volatile static InputStream socketIs;。是对的吗?如何解决?

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) 
        { ..}
...
    }

服务器:

...
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);
...

在尝试to out.writeObject(lot);时,调试在服务器端显示错误 包序列;

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();
        }
    }

}

批号:

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实现Serializable。为什么抛出java.io.NotSerializableException? 我从第一个使用BufferReader(输入流)获取数据的线程成功获得了Lot对象。第二个线程无法读取对象,因为它是第一个线程的红色。看起来像两个线程竞争套接字输入流,尽管事实上InputStream is = socket.getInputStream()变量定义为volatile并且必须确保线程安全协作....

1 个答案:

答案 0 :(得分:1)

在套接字的生命周期中使用单个ObjectInputStream和ObjectOutputStream。