Java中的StreamCorruptedException
我有以下代码:Java中的StreamCorruptedException,java,android,exception,Java,Android,Exception,我有以下代码: public class SenderTask implements Runnable { private DhtDto dto; private ObjectOutputStream oos = null; private Socket socket = null; public SenderTask(DhtDto dto){ this.dto = dto; } @Override public void run() { try{
public class SenderTask implements Runnable {
private DhtDto dto;
private ObjectOutputStream oos = null;
private Socket socket = null;
public SenderTask(DhtDto dto){
this.dto = dto;
}
@Override
public void run() {
try{
socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),dto.sendTo());
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(dto);
oos.close();
socket.close();
oos.reset();
}catch(IOException e){
Log.e("sender","IOException: ",e);
}
}
}
我在下面一行得到一个StreamCorruptedException:
oos.writeObject(dto);
当我搜索时,我看到的答案是,在socket的整个生命周期中,我应该只使用一个ObjectOutputStream。但我不明白这到底意味着什么。有人能详细说明这里的问题是什么以及如何解决吗
提前谢谢。最后,我在同学的帮助下解决了这个问题。想法是在套接字和ObjectOutputStream中间使用BufferedOutputStream。不过,我不太清楚原因