StreamCorruptedException:无效的类型代码:AC [英] StreamCorruptedException: invalid type code: AC

查看:136
本文介绍了StreamCorruptedException:无效的类型代码:AC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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.

Object buf = myInput.readObject();

我假设附加有一些问题,但我真的没有用于追加。我只是想每次都阅读一条新线。
我非常感谢帮助修复这个bug。谢谢。

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.

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

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

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

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:-

@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.

推荐答案

潜在的问题是你使用的是新的 ObjectOutputStream 写入已经使用过的 ObjectOutputStream 的现有 ObjectInputStream 写给。这些流具有由相应构造函数编写和读取的标头,因此如果您创建另一个 ObjectOutputStream ,您将编写一个新标头,该标头以 - 猜猜是什么? - 0xAC,并且现有的 ObjectInputStream 此时并不期望另一个标头,因此barfs。

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

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

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.

如果你想忘记你写的内容,使用 ObjectOutputStream.reset()。

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

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

And don't use any other streams or Readers or Writers on the same socket. The object stream APIs can handle all Java primitive datatypes and all Serializable classes.

这篇关于StreamCorruptedException:无效的类型代码:AC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆