2010-01-31 126 views
1

我正在构建客户端 - 服务器应用程序。现在我想用此代码将消息从客户端转发到所有其他客户端:Java无效流标头:7371007E

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())); 
} 
} 

在客户端,程序正在侦听消息。它引发此异常:

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) 

消息监听:

   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; 
} 

是否有人看到错误?

回答

5

你可能已经得到了你流的扭曲。

当您构造一个ObjectInputStream时,构造函数会从流中读取前两个字节,以期望它们成为应该出现在对象流中的“魔术值”。如果他们不在那里,它会抛出StreamCorruptedException(这全部在ObjectInputStream源代码中)。

因此,看起来你会在ObjectInputStream中包装InputStream,事实上从连接的另一端传出的数据实际上并不是一个对象流。也许它仍然在发送以前的通信数据。

+2

我现在看到我的错误。我改变了构造侦听器线程的方式,但没有意识到'InputStream'仍然是在run()方法中构建的。谢谢! – dododedodonl 2010-01-31 13:14:12