0

RMI を使用してサーバーとクライアント間の通信を実行しようとすると問題が発生します。私のコードは以下に添付されています。サーバーは正常に動作します。PC ポートのリストにリッスン ポートが表示されます。クライアントは registry.lookup で失敗します。同様の問題は見つかりませんでした。助けてください。

サーバ:

public class Main {

private static int port = Registry.REGISTRY_PORT; 
private static Registry registry;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

   if (args.length == 1) {
        try {
                port = Integer.parseInt(args[0]);
                }
            catch (NumberFormatException e) {
    System.out.println("Port number is not valid!");
    System.exit(0);
            }
}

    if (System.getSecurityManager() == null)
        System.setSecurityManager(new RMISecurityManager());
String name = "DBInterface";

    System.out.println("Starting server.");

    try {        
        DBInterface DBInterface = new DBClass();
        DBInterface stub = (DBInterface) UnicastRemoteObject.exportObject(DBInterface, 5000); 
        registry = LocateRegistry.createRegistry(port);
        registry.rebind(name, stub);
    }
    catch (RemoteException e)
    {
        System.out.println(e.getMessage());
    }
    System.out.println("Server succesfully started.");

}

}

クライアント:

public class Client {

private static String ipAddress = "localhost";
private static int port = Registry.REGISTRY_PORT;
private static String confFile = "";
public static DBInterface dbServer = null;
public static String name = "DBInterface";

public static void main(String[] args) {
    System.out.println("Starting client.");

    if (System.getSecurityManager() == null)
        System.setSecurityManager(new RMISecurityManager());


    if (args.length == 3) {
        try {
            ipAddress = args[0];
            port = Integer.parseInt(args[1]);
            confFile = args[2];
        }
        catch (NumberFormatException e) {
    System.out.println("Port number is not valid!");
    System.exit(0);
        }
}
    else
    {
        System.out.println("Some parameter is missing!");
        System.out.println("Valid parameters are: IP(0) Port(1) Config_file_name(2).");
    }

    ArrayList<ArrayList<String>> commands = Tools.loadConfigFile(confFile);
    Iterator<ArrayList<String>> commandsIterator = commands.iterator();
    Registry registry;

try {
        registry = LocateRegistry.getRegistry(ipAddress, port);    
        dbServer = (DBInterface) registry.lookup(name);  //code fails here                   
} catch (RemoteException e) { 
        System.out.println(e.getMessage());  //"no such object in table" is printed on the screen
} catch (NotBoundException e) {
        System.out.println(e.getMessage());
}        
    Tools tools = new Tools(dbServer);

...

4

1 に答える 1

1

What you describe is basically impossible. That exception means that the stub you are using, in this case a Registry, no longer refers to a live exported object in the target JVM. However as Registry stubs are created locally there is no opportunity for them to expire at all. The only mechanism would be the Registry you created exiting, but the static Registry reference should prevent that.

I would change a few things though:

    DBInterface stub = (DBInterface) UnicastRemoteObject.exportObject(DBInterface, 5000); 
    registry = LocateRegistry.createRegistry(port);

Reverse these two lines, and use port for both the Registry and the export. You don't need two ports. Also, in all your catch blocks, never just suppress the exception message in favour of your own. You should always at least log the actual exception message.

于 2013-04-16T02:28:41.053 に答える