簡體   English   中英

Java RMI僅適用於本地主機

[英]Java RMI only working localhost

我正在使用瘦RMI客戶端,但是當我嘗試從除本地主機以外的任何計算機連接到它時,它會獲得拒絕的連接堆棧跟蹤。 我確認防火牆已關閉。 我還需要做什么。 我需要為RMI配置安全性嗎?

Server.java

package com.ibm.icm.autoconfig.server.rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;

public final class Server extends Commands implements ServerHook {
    private Registry registry;

    public static void main(String[] args) throws RemoteException {
        Server server = new Server();
        server.Start();
    }

    private void Start() throws RemoteException {

        ServerHook serverHook = (ServerHook) UnicastRemoteObject.exportObject(this, 0);

        // Add serverHook to the local registry -- attempt to create registry
        // instance, if it fails to create, try finding an existing one
        try {
            registry = LocateRegistry.createRegistry(1099);
        } catch (ExportException ee) {
            registry = LocateRegistry.getRegistry(1099);
        }
        registry.rebind("ServerHook", serverHook);

        System.out.println("Server Running...");

        while (true) {
        }
    }
}

ServerHook.java

package com.ibm.icm.autoconfig.server.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ServerHook extends Remote {
    public boolean write(String msg) throws RemoteException;
}

Client.java

package com.ibm.icm.autoconfig.client.rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import com.ibm.icm.autoconfig.server.rmi.ServerHook;

public class Client {
    private ServerHook serverHook;

    public static void main(String[] args) throws RemoteException, NotBoundException {
        Client client = new Client();
        client.connectTo("localhost");
        client.writeServer("Hello Server!");
    }

    private void connectTo(String serverHost) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry();
        serverHook = (ServerHook) registry.lookup("ServerHook");
    }

    private void writeServer(String msg) throws RemoteException {
        serverHook.write(msg);
    }
}

非本地RMI的Stacktrace:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 9.65.186.135; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at com.ibm.icm.autoconfig.client.rmi.Client.connectTo(Client.java:21)
    at com.ibm.icm.autoconfig.client.rmi.Client.main(Client.java:15)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
    ... 7 more

這是因為下面的客戶端代碼:

client.connectTo("localhost"); --> Client tries to connect to itself. Wrong

這應該更改為:

client.connectTo(serverHost);

其中serverHost是運行服務器代碼的計算機的地址

我在這里找到了問題。 它類似於Suraj Chandran的建議。

private void connectTo(String serverHost) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry();
        serverHook = (ServerHook) registry.lookup("ServerHook");
}

條目LocateRegistry.getRegistry()實際上使我的客戶端指向其自己的注冊表。 正確的代碼是:

private void connectTo(String **serverHost**) throws RemoteException, NotBoundException {
        Registry registry = LocateRegistry.getRegistry(**serverHost**);
        serverHook = (ServerHook) registry.lookup("ServerHook");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM