gpt4 book ai didi

java - 尝试传递 InputStream 时出现 StreamCorruptedException

转载 作者:太空宇宙 更新时间:2023-11-04 08:12:44 25 4
gpt4 key购买 nike

尝试将套接字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);
...

尝试 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 实现了可序列化。为什么抛出java.io.NotSerializedException?我已经从使用 BufferReader(输入流)获取数据的第一个线程成功获取了 Lot 对象。第二个线程无法读取对象,可能是因为它被第一个线程显示为红色。看起来两个线程正在争夺套接字输入流,尽管 InputStream is = socket.getInputStream() 变量定义为 volatile 并且必须确保线程安全协作....

最佳答案

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

关于java - 尝试传递 InputStream 时出现 StreamCorruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com