2013-10-06 73 views
2

我在netbeans中打開了以下文件。無法將RMI客戶端連接到主機,由eofexception導致的unmarshalexception

服務器:

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

客戶端:

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

客戶端存根:

package client; 

import java.rmi.*; 

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

服務器端存根:

package server; 

import java.rmi.*; 

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

服務器類執行罰款,但在運行客戶端類提供了以下錯誤:

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

堆棧跟蹤:

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 

我見過很多具有相同的錯誤消息,但我可以人找不到任何解決方案。任何人都可以幫我嗎?

回答

1

這通常是由對等體的沙箱權限問題引起的。擺脫安全經理。這裏你不需要它。

+0

在卸下安全管理結果誤差解組返回;嵌套的異常是: \t java.lang.ClassNotFoundException:server.ServerInterface(沒有安全管理器:禁用RMI類加載器) –

+0

所以你必須有兩個版本的ServerInterface。因爲客戶甚至不會沒有一個運行。他們需要*相同:*相同的名稱,相同的內容,相同的包,最好是相同的二進制。 – EJP

+0

謝謝,它的包名稱不同! –

0

另一個原因可能是您要傳輸的對象不是真正可序列化的。不幸的是,服務器RMI在默認情況下不記錄這個。檢查的最佳方法是在服務器端返回的對象之前:java.rmi.UnmarshalException:

try { 
    new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(object); 
    } catch (Exception e) { 
    logger.error("serializable check failed", e); 
    } 
相關問題