gpt4 book ai didi

java - 无法将 RMI 客户端连接到主机,由 eofexception 引起的 unmarshalexception

转载 作者:行者123 更新时间:2023-11-30 11:27:44 28 4
gpt4 key购买 nike

我在 netbeans 中打开了以下文件。

服务器:

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

客户端 stub :

package client;

import java.rmi.*;

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

服务器端 stub :

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

我见过很多人有同样的错误消息,但我找不到任何解决方案。这里有人可以帮助我吗?

最佳答案

这通常是由对等方的沙盒权限问题引起的。摆脱安全经理。你在这里不需要它。

关于java - 无法将 RMI 客户端连接到主机,由 eofexception 引起的 unmarshalexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19214479/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com