簡體   English   中英

無法與遠程服務器上的 RMI Registry 建立連接

[英]Cannot establish connection with RMI Registry on a remote server

目前我正在嘗試在遠程機器上使用 RMI 啟動 java 服務器。 服務器似乎已啟動,但我無法在客戶端上調用它的方法。 在客戶端上,我得到一個如下所示的異常:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.0.0.6; nested exception is: 
    java.net.ConnectException: Operation timed out (Connection timed out)

這是我的 RMI 服務器構造函數:

    private RmiServer(String address, int port) throws RemoteException, SQLException, MalformedURLException {
        System.out.println("This address = " + address + ", Port = " + port);
        Registry registry;
        try {
            registry = LocateRegistry.createRegistry(PORT);
        } catch (RemoteException e)
        {
            registry = LocateRegistry.getRegistry(PORT);
        }
        System.setProperty("java.rmi.server.hostname", address);
        try {
            registry.bind(REGISTRY_NAME, this);
            Naming.bind(address, this);
        }
        catch (AlreadyBoundException e)
        {
            registry.rebind(REGISTRY_NAME, this);
            Naming.rebind(address, this);
        }
        ...
    }

這是我的客戶代碼:

    static public void main(String[] args) throws IOException {
        try {
            Registry registry = LocateRegistry.getRegistry(SERVER_ADDRESS, SERVER_PORT);
            rmiServer = (ReceiveMessageInterface) registry.lookup(REGISTRY_NAME);
        } catch (RemoteException | NotBoundException e) {
            System.err.println(e.getMessage());
            isServerOK = false;
        }
        Scanner reader = new Scanner(new InputStreamReader(System.in));
        boolean isAuthenticated = false;
        int pin;
        String cardNumber;
        SessionInfo session = null;
        if (isServerOK) {
            while (true) {
                if (!isAuthenticated) {
                    System.out.println("Welcome Customer to HDFC Bank ATM : \nPlease enter your card number: ");
                    cardNumber = reader.next();
                    System.out.println("Please enter your PIN: ");
                    pin = reader.nextInt();
                    String cardHolderName = rmiServer.verifyPIN(cardNumber, pin);
                    ...
                }
                ...
            }
            ...
        }
        ...
    }

我用這個字符串啟動服務器:

java -cp flyway-core-6.0.8.jar:mssql-jdbc-7.4.1.jre8.jar:mysql-connector-java-8.0.18.jar:. -Djava.security.policy=java.security.AllPermission kmalfa.RmiServer

我的服務器在 linux 虛擬機上運行。 我使用 Microsoft Azure 服務。 我關閉了客戶端和虛擬機上的防火牆。 我允許 Azure 設置中的所有傳入連接用於測試目的,但沒有幫助。 可能是什么問題呢?

解決了。

只好打電話

System.setProperty("java.rmi.server.hostname", address);

不在RmiServer構造函數中,而是在調用它之前,在main的開頭

暫無
暫無

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

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