我的客户端和服务器都有这个类
public class user implements Serializable{
int pass;
String name;
public user(int pass, String name){
this.pass = pass;
this.name=name;
}//i also have a getPass and a getName
以下是我如何设置Streams
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n The streams are now set up! \n");
}
现在我要做的是将用户对象从客户端发送到服务器 这是我在客户端做的事情:
button1l.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
if(isLogin(id)==true){
User user1 = new User(123,"test");
try {
output.writeObject(user1);
output.flush();
} catch (IOException ex) {
Logger.getLogger(ClientClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
);
并在服务器端:
public void run() {
sendMessage("hello there\n");
do{
try{
System.out.println("before\n");//everytime loop it reaches here
User user1 = (User) input.readObject(); //but never here
System.out.println("read user\n");
System.out.println("x="+ user1.getPass()+ "y=" + user1.getName());
}catch(ClassNotFoundException classNotFoundException){
System.out.println(classNotFoundException);//i just added this println
} catch (IOException ex) {
Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
}
}while(true);
}
但没有任何反应。没有错误只是一个空白的屏幕。那么我做错了什么或者这是错误的做法? PS:添加println后(classNotFoundException);我每次尝试发送的打印是:java.lang.ClassNotFoundException:client.User ..
答案 0 :(得分:0)
问题似乎是User
类位于服务器和客户端程序的不同包中。
为了将此类的实例视为类似对象,该类必须位于两个程序中具有相同名称的包中。