Java StreamCorruptedException:无效的类型代码:AC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2393179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:55:54  来源:igfitidea点击:

StreamCorruptedException: invalid type code: AC

javamultithreadingstreamingobject

提问by leba-lev

My problem is when it tries to read the object the second time, it throws the exception:

我的问题是当它第二次尝试读取对象时,它抛出异常:

java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at Client.run(BaseStaInstance.java:313)

java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at Client.run(BaseStaInstance.java:313)

The first time I send the exact same object message; however, when I try doing the same thing the second time, it throws the error above. Do I need to re-intialize the readObject() method? I even printed out the message object that is being received by the line below and its exact the same as the first instance where it works ok.

我第一次发送完全相同的对象消息;但是,当我第二次尝试做同样的事情时,它会抛出上面的错误。我需要重新初始化 readObject() 方法吗?我什至打印出下面一行接收的消息对象,它与第一个正常工作的实例完全相同。

Object buf = myInput.readObject();

I'm assuming there's some problem with appending, but I really have no use for appending. I just want to read a fresh line everytime. I'd really appreciate some help in fixing this bug. Thank you.

我假设附加有一些问题,但我真的没有附加的用处。我只想每次都读新的一行。我真的很感激修复这个错误的一些帮助。谢谢你。

==================================

==================================

Before that one line, I'm just creating the input and output objects for the socket in the run() method. The object declaration is outside the run() method in the class:-

在这一行之前,我只是在 run() 方法中为套接字创建输入和输出对象。对象声明在类中的 run() 方法之外:-

@Override
public void run() {
    try {
        sleep((int) 1 * 8000);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        //Creating input and output streams to transfer messages to the server
        myOutput = new ObjectOutputStream(skt.getOutputStream());
        myInput = new ObjectInputStream(skt.getInputStream());
        while (true) {
            buf = myInput.readObject();
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You're right; I don't close the object. I'm not sure how to do that.

你是对的; 我不关闭对象。我不知道该怎么做。

回答by user207421

The underlying problem is that you are using a new ObjectOutputStreamto write to an existing ObjectInputStreamthat you have already used a prior ObjectOutputStreamto write to. These streams have headers which are written and read by the respective constructors, so if you create another ObjectOutputStreamyou will write a new header, which starts with - guess what? - 0xAC,and the existing ObjectInputStreamisn't expecting another header at this point so it barfs.

潜在的问题是您正在使用 newObjectOutputStream来写入ObjectInputStream您已经使用过的现有ObjectOutputStream写入。这些流具有由相应构造函数写入和读取的标头,因此如果您创建另一个流,ObjectOutputStream您将编写一个新标头,该标头以 - 你猜怎么着?-0xAC,并且现有的ObjectInputStream在这一点上并不期待另一个标题,所以它会呕吐。

In the Java Forums thread cited by @trashgod, I should have left out the part about 'anew for each object at both ends': that's just wasteful. Use a single OOS and OIS for the life of the socket, and don't use any other streams on the socket.

在@trashgod 引用的 Java 论坛线程中,我应该省略关于“两端的每个对象都重新创建”的部分:这只是浪费。在套接字的生命周期内使用单个 OOS 和 OIS,并且不要在套接字上使用任何其他流。

If you want to forget what you've written, use ObjectOutputStream.reset().

如果您想忘记所写的内容,请使用 ObjectOutputStream.reset().

And don't use any other streams or Readersor Writerson the same socket. The object stream APIs can handle all Java primitive datatypes and all Serializableclasses.

并且不要在同一个套接字上使用任何其他流或ReadersWriters。对象流 API 可以处理所有 Java 原始数据类型和所有Serializable类。