0

我是 RMI 技术的新手,面临以下问题。

我们有多个相同类型的设备连接到本地系统,每个设备上的 RMI 服务在不同的端口上运行。

当我们尝试通过 RMI 将单个设备连接到本地系统时,它工作正常。当我们尝试将第二台设备连接到本地系统时,我们收到如下错误 -

您能帮我们解决以下问题吗?

提前致谢。



    java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
        java.net.ConnectException: Connection refused: connect
        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.newCall(UnicastRef.java:322)
        at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
        at java.rmi.Naming.rebind(Naming.java:160)
        at com.rmi.server.RMIServer.exportAndBindObject(Unknown Source)


演示.java



    this.myRMIServer = new RMIServer(this.RMIServerPort,this.RMIClientPort, new RMISocketFactory());
    this.helloWorld = new HelloWorld();
    this.myRMIServer.exportObject(this.helloWorld);
    this.myRMIServer.exportAndBindObject(this.rmiServiceName, this.helloWorld);

RMIServer.java



    public RMIServer(int port, int rmiPort, java.rmi.server.RMISocketFactory sf)
            throws RemoteException {
        this.sf = sf;
        this.rmiPort = rmiPort;
        this.regPort = port;
        synchronized (this) {
            if (registry == null)
                registry = LocateRegistry.createRegistry(port);
        }
    }

    public void exportAndBindObject(String name, RemoteObject ro)
            throws RemoteException, MalformedURLException {
        exportObject(ro);
        String url = "//127.0.0.1:" + this.regPort + "/" + name;
        Naming.rebind(url, ro);
    }

4

1 回答 1

1

您正在创建注册表,但在找不到它的地方port绑定到它。regPort

我不知道this.RMIClientPort可能的目的是什么。我会摆脱它。RMI 服务器无需考虑客户端端口。

此外,您要导出HelloWorld对象两次:一次在您调用的 Demo.java 中exportObject(),一次在RMIServer.exportAndBindObject()您再次调用 exportObject() 的位置。因此,其中一项操作一定失败了,否则它不会导出任何东西。所以你的exportObject()方法有问题。

于 2012-06-08T00:58:46.703 回答