RMI客户端显示java.RMI.NotBoundException错误



请帮助我编写代码-rmi客户端显示错误,但服务器运行正常。我已经运行了服务器,它运行正常,但客户端抛出了一个java.rmi.NotBoundException,我不知道错误是客户端代码的哪一部分

这是客户端代码

package GUI;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import rmiInterface.LoginInterface;
public class LaunchGUI {
private JFrame frame;
private LoginInterface myService;
LoginGUI login;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LaunchGUI window = new LaunchGUI(); // 3
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
String mySessionCookie = "not set";
public LaunchGUI() {
initialize(); // 2
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
try {
myService = (LoginInterface) Naming.lookup("rmi://localhost/Service");// 1
} catch (Exception e) {
System.out.println("A problem occured: " + e.toString());
e.printStackTrace();
}
JButton btnNewButton = new JButton("Sign In");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// login = new LoginGUI();
String str = JOptionPane.showInputDialog("Please enter the password.");
try {
String result = myService.login(str);
if (result.equals("wrong")) {
System.out.println("Wonrg Password. Try again!");
} else {
mySessionCookie = result;
System.out.println("Your login was successful.");
}
} catch (Exception ex) {
System.out.println("A problem occured: " + ex.toString());
ex.printStackTrace();
}
}
});
btnNewButton.setBounds(24, 144, 97, 25);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Sign Out");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String result = myService.logout(mySessionCookie);
System.out.println("Logout: " + result);
} catch (Exception ex) {
System.out.println("A problem occured: " + ex.toString());
ex.printStackTrace();
}
}
});
btnNewButton_1.setBounds(297, 144, 97, 25);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("View Score");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result;
try {
result = myService.sayHello();
System.out.println("Result: " + result);
} catch (RemoteException ex) {
System.out.println("A problem occured: " + ex.toString());
ex.printStackTrace();
}
}
});
btnNewButton_2.setBounds(157, 199, 97, 25);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("Start Game");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String result = myService.getSecretMessage(mySessionCookie);
System.out.println("Result: " + result);
} catch (Exception ex) {
System.out.println("A problem occured: " + ex.toString());
ex.printStackTrace();
}
}
});
btnNewButton_3.setBounds(157, 144, 97, 25);
frame.getContentPane().add(btnNewButton_3);
}
}

这是rmiInterface

package rmiInterface;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface LoginInterface extends Remote {
public String sayHello() throws RemoteException;
public String login(String password) throws RemoteException;
public String getSecretMessage(String cookie) throws RemoteException;
public String logout(String cookie) throws RemoteException;
}

这是服务器代码

package Server;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
System.out.println("Attempting to start the Login Server...");
try {
Service myHello = new Service();
Registry reg = LocateRegistry.createRegistry(1076);
reg.rebind("Service", myHello);
System.out.println("Service started. Welcome to the RMI Login Service!");
} catch (RemoteException e) {
System.out.println("An error occured: " + e.toString());
e.printStackTrace();
}
}
}

这是服务代码

package Server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import rmiInterface.LoginInterface;
public class Service extends UnicastRemoteObject implements LoginInterface {
/**
* 
*/
private static final long serialVersionUID = 1L;
private String sessionCookie = "abc" + Math.random();
public Service() throws RemoteException{
// TODO Auto-generated constructor stub
}
/*
* @Override public String sayHello() throws RemoteException {
* 
* return "Hello! Have a good day!"; }
*/
@Override
public String getSecretMessage(String cookie) throws RemoteException {
if (cookie.equals(sessionCookie)) {
return "This is a secret message: Alice is in a relationship with Bob. It is complicated.";
} else {
return "You must login to read this message";
}
}
@Override
public String login(String password) throws RemoteException {
/*
* Note and Warning! This setup demonstrates the interaction between cookies and
* login. Actually security is not in the scope of this demo. Usually you
* wouldn't hardcode a password in production code but the password were to be
* checked up against an (encrypted) database.
*/
if (password != null && password.equals("hello")) {
sessionCookie = "xyz" + Math.random();
// timeout(5000)//my code so heck recording for answer :)
return sessionCookie;
} else {
return "wrong";
}
}
@Override
public String sayHello() throws RemoteException {
return "Hello! Have a good day!";
}
public String logout(String cookie) throws RemoteException {
sessionCookie = "abc" + Math.random();
return "logout successful";
}
/*
* @Override public String logout(String cookie) throws RemoteException {
* sessionCookie = "abc" + Math.random(); return "logout successful"; }
*/
}

此客户端显示java.rmi.NotBoundException错误,请帮助!!!!

您正在混合不同类型的连接。要连接到RMI服务,您应该将以下服务器代码配对:

Registry reg = LocateRegistry.createRegistry(port);
reg.rebind("Service", myHello);

客户端代码:

Registry reg = LocateRegistry.getRegistry(port);
Blah stub = (Blah) reg.lookup("Service");

或者,如果您已通过以下方式启动rmiregistry

set CLASSPATH=/path/to/your-client.jar
rmiregistry <port>

然后这个服务器代码:

Naming.rebind("rmi://"+hostname+":"+port+"/"+name, stub);

由客户端代码匹配:

Blah stub = (Blah) Naming.lookup("rmi://"+hostname+":"+port+"/"+name);

最新更新