我有两个包,一个用于客户端,一个用于服务器。 Message.java文件存在于两个包中。
// Message.java
import java.io.Serializable;
public class Message implements Serializable{
private static final long serialVersionUID = 1L; //*** This line is present only on server side.
public String type, sender, content, recipient;
public Message(String type, String sender, String content, String recipient){
this.type = type;
this.sender = sender;
this.content = content;
this.recipient = recipient;
}
public String toString(){
return "{type='"+type+"', sender='"+sender+"', content='"+content+"', recipient='"+recipient+"'}";
}
}
在客户端,我有以下代码
public void send(Message msg){
try{
Out.writeObject(msg);
Out.flush();
}catch(Exception e){
System.out.println("Exception SocketClient send()");
}
}
在服务器端,我有以下代码
public void run(){
while (true){
try{
Message msg = (Message) streamIn.readObject();
}
catch(Exception ioe){
System.out.println(ID + " ERROR reading: " + ioe.getMessage());
}
}
}
在线“Message msg =(Message)streamIn.readObject();”在服务器端我得到 “ClassCastException异常”。请帮助,因为我被困在这里!