Java无效流标头:7371007E [英] Java invalid stream header: 7371007E
问题描述
我正在构建客户端服务器应用程序.现在,我要使用以下代码将邮件从客户端转发到所有其他客户端:
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:
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;
}
有人看到错误了吗?
推荐答案
您的流媒体可能已经曲折了.
You've likely got your streams in a twist.
当构造ObjectInputStream
时,构造函数从流中读取前两个字节,期望它们是对象流中应该存在的魔术值".如果不存在,则会抛出StreamCorruptedException
(这全部在ObjectInputStream
源代码中).
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 ObjectInputStream
source code).
因此,实际上从连接另一端传来的数据实际上不是对象流时,似乎是在ObjectInputStream
中包装了InputStream
.也许它仍在从先前的通信中发送数据.
So it would appear that you're wrapper an InputStream
in an ObjectInputStream
when 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.
这篇关于Java无效流标头:7371007E的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!