0

The following client code accepts a "Guest" object from the server:

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());
    }
}

And on the server side it looks like this:

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());
    }

}

From the client side it returns a ClassNotFoundException at the line "g = (Guest) ois.readObject();":

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)

The fact that the error message says "server.Guest", ie (class package).(object), can I assume that an instance of the server's Guest class isn't the same thing as an instance of the client's Guest class? So how do I properly send a receive objects through a socket?

Thanks

1 Answers1

0

The fact that the error message says "server.Guest", ie (class package).(object), can I assume that an instance of the server's Guest class isn't the same thing as an instance of the client's Guest class?

That two separate classes are involved is what the exception is telling you, yes.

So how do I properly send a receive objects through a socket?

Limit yourself to classes that client and server both know. There is a variety of ways to accomplish this, but they pretty much all involve deploying the same or equivalent .class files on both sides of the connection. "Equivlent" here means approximately "compiled from the same source with compilers of the same version".

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • How do I share 'equivalent' classes between 2 programs that are assumed to be able to communicate and share instances of the same class non-locally? – AyyshKeychumm Jan 16 '19 at 21:42
  • @AyyshKeychumm, Typically, you package the class with both the client program and the server program, so that both naturally have access to it. This does require client and server to agree beforehand about the classes of objects that can be exchanged, but that's not really different from what any other communication medium requires. In any case, if this still doesn't make sense to you then you're going to need to be more specific about the nature of your confusion. – John Bollinger Jan 16 '19 at 21:48
  • No I think I understand now. Thanks for the advice John! – AyyshKeychumm Jan 16 '19 at 22:07