rmi - ConnectException

时间:2013-01-10 03:35:18

标签: rmi

这是我第一次使用RMI,基本上我设法在我的PC上本地运行以下RMI示例,但不是通过两台独立的Linux机器。

服务器界面:

public interface PowerService extends Remote{
public BigInteger square ( int number )
    throws RemoteException;

public BigInteger power  ( int num1, int num2) 
    throws RemoteException;
}

服务器:

public class PowerServiceServer extends UnicastRemoteObject implements
    PowerService {

public PowerServiceServer() throws RemoteException {
    super();
}

public BigInteger square(int number) throws RemoteException {
    imp .....
    return (bi);
}

public BigInteger power(int num1, int num2) throws RemoteException {
    imp .....
    return bi;
}

public static void main(String[] args) throws Exception {

    PowerServiceServer svr = new PowerServiceServer();
    // ... and bind it with the RMI Registry
    Naming.bind("PowerService", svr);
    System.out.println("Service bound....");
}
}

客户:

public class PowerServiceClient {
public static void main(String args[]) throws Exception {
    // Call registry for PowerService
    PowerService service = (PowerService) Naming.lookup("rmi://" + args[0]
            + "/PowerService");
    DataInputStream din = new DataInputStream(System.in);
    for (;;) {
        System.out.println("1 - Calculate square");
        System.out.println("2 - Calculate power");
        System.out.println("3 - Exit");
        System.out.println();
        System.out.print("Choice : ");

        String line = din.readLine();
        Integer choice = new Integer(line);

        int value = choice.intValue();

        switch (value) {
        case 1:
            // Call remote method
            ....................
            break;
        case 2:
            // Call remote method
            ....................
            break;
        case 3:
            System.exit(0);
        default:
            System.out.println("Invalid option");
            break;
        }
    }
}

并且客户端接口与服务器

相同

这是我为了运行rmi示例所做的:

1)在服务器端,我创建了存根

2)运行rmiregisrty

3)运行服务器

4)我将存根从服务器端复制到客户端到同一个包

5)运行客户端

运行客户端后,我收到以下错误消息:

线程“main”中的异常java.rmi.ConnectException:Connection拒绝主机:127.0.0.1;嵌套异常是:         java.net.ConnectException:连接被拒绝         at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)         at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)         at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)         at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:110)         at compute.PowerServiceServer_Stub.square(未知来源)

是可能的,由于某些防火墙我无法连接或者我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

这是java.rmi.server.hostname.解决的问题。请参阅RMI常见问题解答中的第A.1项。您需要修复导致它的/ etc / hosts配置错误,或者在导出任何远程对象之前将服务器JVM中的系统属性java.rmi.server.hostname设置为服务器的正确IP地址。