我有一个奇怪的问题。我正在尝试编写带有聊天选项的简单客户端 - 服务器应用程序,但我遇到了一个问题 - 对象输入流读取错误的类。错误是:
Exception in thread "Thread-4" java.lang.ClassCastException: org.oxomoco.packets.PacketAlive cannot be cast to org.oxomoco.packets.PacketMessage
at org.oxomoco.server.SocketConnectionServer.run(SocketConnectionServer.java:52)
at java.lang.Thread.run(Unknown Source)
它发生在这里:
PacketMessage pm = (PacketMessage)ois.readObject();
在循环中看起来像这样:
while(true){
//if(ois.readObject() instanceof PacketMessage){
// pm = (PacketMessage)ois.readObject();
// se.println(ois.readObject().toString());
//}
if((ois.readObject() instanceof PacketMessage)==true){
System.out.println("It is: " + (ois.readObject() instanceof PacketMessage));
PacketMessage pm = (PacketMessage)ois.readObject();
se.acprintln(">>",uname + ": " +pm.getMsg());
}
if((ois.readObject() instanceof PacketAlive)==true){
System.out.println("-it is: " + (ois.readObject() instanceof PacketAlive));
PacketAlive pa = (PacketAlive)ois.readObject();
}
}
不知何故 - 即使我检查实例 - 对象输入流是 PacketMessage 的实例,而它应该是 PacketAlive 的实例。有谁知道为什么,当然 - 如何解决它?