以下客户端代码从服务器接受“来宾”对象:
protected static void sendLoginRequest(String email, String password) {
try(
Socket socket = new Socket("127.0.0.1", 44444);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
){
out.write("LGNR");
out.flush();
out.newLine();
out.flush();
out.write(email);
out.flush();
out.newLine();
out.flush();
out.write(password);
out.flush();
out.newLine();
out.flush();
in.ready();
switch(in.readLine()) {
case "SU":
Guest g = null;
try(ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());){
g = (Guest) ois.readObject();
}catch(ClassNotFoundException | IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
GuestProfileMenu.generateMenu(g);
break;
case "WP":
System.out.println("Invalid Password.");
break;
case "IE":
System.out.println("Email not found.");
break;
}
}catch(IOException e) {
System.out.println("No response from Server.");
System.out.println(e.getMessage());
}
}
在服务器端,它看起来像这样:
private void userLogin(Socket socket, BufferedReader in, BufferedWriter out) {
System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - has sent a login request.\r\n");
FileManager.openGuestFile();
ArrayList<Guest> gList = FileManager.getGuestList();
try{
in.ready();
String email = in.readLine();
System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - Email received as: " + email);
in.ready();
String password = in.readLine();
System.out.println(socket.getInetAddress().getHostName() + ":" + socket.getLocalPort() + " - Password received as: " + password);
boolean exists = false;
for(int i = 0; i < gList.size(); i++) {
if (email.equals(gList.get(i).getEmail())) {
exists = true;
if (password.equals(gList.get(i).getPassword())) {
out.write("SU");
out.flush();
out.newLine();
out.flush();
try(ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());){
oos.writeObject(gList.get(i));
oos.flush();
}catch(IOException e) {
System.out.println(e.getMessage());
}
}else{
out.write("WP");
out.flush();
out.newLine();
out.flush();
}
}
}
if(!exists) {
out.write("IE");
out.flush();
out.newLine();
out.flush();
}
socket.close();
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
从客户端,它在“ g =(Guest)ois.readObject();”行返回ClassNotFoundException:
java.lang.ClassNotFoundException: server.Guest
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
错误消息显示“ server.Guest”(即(类包)。(对象))的事实,我可以假定服务器的Guest类的实例与客户端的Guest的实例不同类?那么如何正确地通过套接字发送接收对象呢?
谢谢
答案 0 :(得分:0)
错误消息显示“ server.Guest”的事实,即(类 包)(对象),我可以假设服务器的Guest实例 类与客户端的Guest类的实例不同吗?
是的,异常告诉您,涉及到两个单独的类。
那我如何通过套接字正确发送接收对象?
将自己限制在客户端和服务器都知道的类上。有多种方法可以实现此目的,但是几乎所有方法都涉及在连接的两侧部署相同或等效的.class文件。这里的“等效”是指大约“从同一来源使用相同版本的编译器进行编译”。