不同机器上的Java RMI客户端/服务器

时间:2015-03-06 04:51:44

标签: java rmi

我在运行客户端时收到以下错误消息:

HelloClient exception: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
java.net.ConnectException: Connection refused

我试图在不同的机器上运行我的客户端/服务器,但我没有运气。

这是我的代码:

FrontEndServer.java:

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Vector;
import java.util.Iterator;

public class FrontEndServer implements FrontEndInterface {

    static TestRemote catalog;
    public FrontEndServer() {
        super();
    }

    public String bookToString(Book b) {
        String response = "Title: " + b.title + "\n" 
                        + "Item #: " + Integer.toString(b.itemNumber) + "\n"
                        + "Price: " + Float.toString(b.price) + "\n"
                        + "Category: " + b.topic + "\n"
                        + "# in Stock: " + Integer.toString(b.stock);
        return response;
    }

    public String parseCommand(String m) {

        return m;
    }

    public static void main(String argv[]) {


        try {

            // connect to Client
            String nameForClient = "Command";
            FrontEndServer engine = new FrontEndServer();
            FrontEndInterface stub =
                (FrontEndInterface) UnicastRemoteObject.exportObject(engine, 0);
            Registry registry = LocateRegistry.getRegistry(8080);
            registry.rebind("Command", stub);

            System.out.println("Server is running...");

        } catch(Exception e) {
            System.out.println("Hello Server failed: " + e);
        }
    }
}

Client.java:

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

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

        boolean done = false;
        Scanner reader = new Scanner(System.in);
        System.out.println("Welcome to Niles.com!");
        System.out.println("To search for something by topic or category, type 'search x', where x is what you want to search");
        System.out.println("To look for something by item number, type 'lookup x', where x is what you want to look up");
        System.out.println("To buy something, type 'buy x', where x is what you want to buy");
        System.out.println("To quit, type 'quit'");

        try {

            if (args.length < 0) {
                System.out.println("usage: java HelloClient string...\n");
                System.exit(1);
            }

            if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
            }


            // String name = "Command";
         //    Registry registry = LocateRegistry.getRegistry(args[0]);
         //    FrontEndInterface hello = (FrontEndInterface) registry.lookup(name);

            Registry registry = LocateRegistry.getRegistry(args[0], 8080);
            FrontEndInterface stub = (FrontEndInterface) registry.lookup("Command");

            while(!done) {
                System.out.println("\n" + "What would you like to do?");
                String command = reader.nextLine();

                // make sure command is 2 words (automaticly invalid if not)
                if (command == null || command.equalsIgnoreCase("quit")) {
                    // client wants to stop shopping
                    done = true;
                    System.out.println("Goodbye! Thanks for shopping with us!");
                } else {
                    System.out.println(stub.parseCommand(command));
                }

            }

        } catch (Exception e) {
            System.out.println("HelloClient exception: " + e);
        }

    }
}

思想??

0 个答案:

没有答案