Java 如何将单个对象绑定到多个RMI服务器

Java 如何将单个对象绑定到多个RMI服务器,java,rmi,Java,Rmi,我是RMI技术新手,面临以下问题 我们有多个相同类型的设备连接到本地系统,在每个设备上,RMI服务在不同的端口上运行 当我们试图通过RMI将单个设备连接到本地系统时,它工作正常。 当我们尝试将第二个设备连接到本地系统时,我们得到如下错误- 您能帮我们解决以下问题吗 提前谢谢 java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: java.net.C

我是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)

Demo.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);



    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);
    }

RMIServer.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);



    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);
    }


您正在
端口
上创建注册表,但在
regPort
上绑定到它,但找不到它

我不知道
这个.RMIClientPort的目的可能是什么。我会把它处理掉的。RMI服务器不需要考虑客户端端口


您还将导出
HelloWorld
对象两次:一次在Demo.java中调用
exportObject()
,一次在
RMIServer.exportAndBindObject()
中再次调用exportObject()。因此,其中一个操作一定失败了,否则它不会导出任何内容。因此,您的
exportObject()
方法有问题。

我看不出您的标题和您的问题之间有任何关系,我无法相信此代码在任何系统上都有效,除非端口相等:请参阅我的答案。