Java invalid stream header: 7371007E

别说谁变了你拦得住时间么 提交于 2020-01-05 00:36:11

问题


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?


回答1:


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 ObjectInputStream source code).

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.



来源:https://stackoverflow.com/questions/2171635/java-invalid-stream-header-7371007e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!