Java RMI 仅在本地主机上工作

标签 java exception connection localhost rmi

我正在开发一个瘦 RMI 客户端,但是当我尝试从本地主机以外的任何计算机连接到它时,它会收到拒绝的连接堆栈跟踪。我确认我的防火墙已关闭。我还需要做什么。我需要为 RMI 配置安全性吗?

服务器.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;
}

客户端.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 的堆栈跟踪:

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 是运行服务器代码的计算机的地址

关于Java RMI 仅在本地主机上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11744384/

相关文章:

iphone - 将 helpshift 添加到应用程序导致 UIApplicationInvalidInterfaceOrientation 异常

C套接字编程未输入端口

java - 使用 DbUtils QueryRunner 不会关闭数据库连接

java - 不唯一的表/别名 : in query Java program

java - 构建缓存失败应该抛出哪个异常?

c++ - 使用SWIG将自定义C++异常动态地重新抛出为Python异常

php - 在 PHP 中通过 SSL 连接到 AWS mysql?

java - 如何从保存为字符串的文件路径中获取特定部分 - Android/Java

java - 清除 Recyclerview 的数据(ArrayList)后,EndlessRecyclerOnScrollListener 的 onLoadMore 事件未触发

java - JTable 和 ListSelectionListener