Unable to connect RMI client to host, unmarshalexception caused by eofexception

Ashley Stewart picture Ashley Stewart · Oct 7, 2013 · Viewed 8k times · Source

I have the following files open in netbeans.

server:

package server;

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.sql.*;

public class Server implements ServerInterface {

    private Connection con;
    private Statement st;

    public Server(String db, String username, String password) {
        try {
            con = DriverManager.getConnection(db, username, password);
            st = con.createStatement();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        try {
            Server server = new Server("jdbc:mysql://localhost:3306/db", "root", "password");
            System.setSecurityManager(new RMISecurityManager());
            ServerInterface stub = (ServerInterface) UnicastRemoteObject.exportObject(server, 0);
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("Server", stub);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public boolean authenticate(String username, String password) {
        try {
            String query = "SELECT * FROM staff WHERE staff_id ='" + username + "' AND password = '" + password + "'";
            ResultSet rs = st.executeQuery(query);
            if(rs.next()) {
                return true;
            } else {
                return false;
            }
        } catch(Exception e) {
            System.out.println(e);
        }
        return false;
    }
}

client:

package client;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.rmi.registry.*;

public class Client extends JFrame implements ActionListener {

    JFrame main;
    JPanel loginPanel;
    JPanel contentPanel;
    JButton horse;
    JButton staff;
    JButton loan;
    JButton treatment_record;
    JButton work_record;
    JButton contact;
    JButton loaner;
    JButton field_visit;
    JButton login;
    JTextField username;
    JPasswordField password;
    String usernameString;
    String passwordString;

    public Client() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println(e);
        }

        loginPanel = new JPanel();
        contentPanel = new JPanel();
        login = new JButton("Login");
        username = new JTextField(20);
        password = new JPasswordField(20);
        horse = new JButton("Horses");
        staff = new JButton("Staff");
        loan = new JButton("Current Loans");
        treatment_record = new JButton("Treatment Records");
        work_record = new JButton("Work Records");
        contact = new JButton("Contacts");
        loaner = new JButton("Loaners");
        field_visit = new JButton("Field Visits");

        horse.addActionListener(this);
        staff.addActionListener(this);
        loan.addActionListener(this);
        treatment_record.addActionListener(this);
        work_record.addActionListener(this);
        contact.addActionListener(this);
        loaner.addActionListener(this);
        field_visit.addActionListener(this);
        login.addActionListener(this);

        loginPanel.add(new JLabel("Username"));
        loginPanel.add(username);
        loginPanel.add(new JLabel("Password"));
        loginPanel.add(password);
        loginPanel.add(login);
        contentPanel.add(horse);
        contentPanel.add(staff);
        contentPanel.add(loan);
        contentPanel.add(treatment_record);
        contentPanel.add(work_record);
        contentPanel.add(contact);
        contentPanel.add(loaner);
        contentPanel.add(field_visit);
        contentPanel.setLayout(new GridLayout(0, 3));
        loginPanel.setLayout(new GridLayout(0, 2));
        contentPanel.setSize(800, 600);

        this.setTitle("TRC Database");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setContentPane(loginPanel);
        this.setVisible(true);   
        this.pack();
    }

    public static void main(String[] args) {
        new Client();

        try {
            Registry registry = LocateRegistry.getRegistry();
            ServerInterface stub = (ServerInterface) registry.lookup("Server");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == login) {
            usernameString = username.getText();
            passwordString = password.getText();
            JOptionPane.showMessageDialog(null, "Button pressed!");
        }
        this.revalidate();
        this.pack();
    }
}

client side stub:

package client;

import java.rmi.*;

public interface ServerInterface extends Remote {
    public boolean authenticate(String username, String password) throws RemoteException;
}

server side stub:

package server;

import java.rmi.*;

public interface ServerInterface extends Remote {
    public boolean authenticate(String username, String password) throws RemoteException;
}

The server class executes fine, but running the client class gives the following error:

java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: 
    java.io.EOFException

Stacktrace:

java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: 
    java.io.EOFException
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:227)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at client.Client.main(Client.java:88)
Caused by: java.io.EOFException
    at java.io.DataInputStream.readByte(DataInputStream.java:267)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:213)
    ... 3 more

I've seen a lot of people having the same error message but I can't find any solutions posted to it. Can anyone here please help me?

Answer

user207421 picture user207421 · Oct 7, 2013

This is usually caused by sandbox permission problems at the peer. Get rid of the security manager. You don't need it here.