Java 无效流标头:7371007E

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2171635/
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-10-29 19:50:10  来源:igfitidea点击:

Java invalid stream header: 7371007E

javaserializationio

提问by dododedodonl

I am building a client-server application. Now I want to forward the message from a client to all other client with this code:

我正在构建一个客户端 - 服务器应用程序。现在我想使用以下代码将消息从客户端转发到所有其他客户端:

ArrayList<User> usrs = _usrHandler.getUsers();
for(User usr : usrs) {
    if(!usr.getSocket().equals(_connection)) {
        usr._oOut.writeObject(new CommunicationMessage(this._comMsg.getMessage(), CommunicationMessage.MSG, 
                                                    this._comMsg.getUser()));
 }
}

On the client side the program is listening for messages. It throws this exception:

在客户端,程序正在侦听消息。它抛出这个异常:

java.io.StreamCorruptedException: invalid stream header: 7371007E
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
    at Connection$MessageListener.run(Connection.java:126)
    at java.lang.Thread.run(Thread.java:637)

MessageListener:

消息监听器:

             while(this._loop) {
 this._comMsg = (CommunicationMessage) this._dataInput.readObject();

 SimpleAttributeSet attr = new SimpleAttributeSet();
 attr.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
 attr.addAttribute(StyleConstants.CharacterConstants.Foreground, _comMsg.getUser().getColor());

 messageClient.addMessage(_comMsg.getUser().getNickName() + ": ", attr);
 messageClient.addMessage(_comMsg.getMessage(), _comMsg.getUser().getColor());

 _comMsg = null;
}

Does someone see the error?

有人看到错误吗?

回答by skaffman

You've likely got your streams in a twist.

您可能已经将您的信息流弄得一团糟。

When you construct an ObjectInputStream, the constructor reads the first two bytes from the stream expecting them to be the "magic values" that should be present in an object stream. If they're not there, it throws the StreamCorruptedException(this is all in the ObjectInputStreamsource code).

当您构造一个 时ObjectInputStream,构造函数从流中读取前两个字节,期望它们是对象流中应该存在的“魔法值”。如果它们不存在,它会抛出StreamCorruptedException(这都在ObjectInputStream源代码中)。

So it would appear that you're wrapper an InputStreamin an ObjectInputStreamwhen in fact the data coming down from the other end of the connection is not actually an object stream. Perhaps it's still sending data from a previous communication.

因此,看起来您正在包装 an InputStreamObjectInputStream而实际上来自连接另一端的数据实际上并不是对象流。也许它仍在发送来自先前通信的数据。