【问题标题】:java RMI over the internet. host refuse connection互联网上的java RMI。主机拒绝连接
【发布时间】:2014-11-21 22:50:44
【问题描述】:

我们正试图让 RMI 在 Internet 上工作。我们尝试了什么:

  1. 客户端和服务器端的端口转发 (1099-1100)。
  2. 关闭 windows 和路由器中的防火墙
  3. 用 tunngle (www.tunngle.net/) 尝试过

我们的 RMI 接口:

import java.rmi.RemoteException;

public interface RMIInterface extends java.rmi.Remote    {
  public void helloWorld(int i) throws RemoteException;
}

我们的 RMI 服务器实现:

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RMIServerTest extends UnicastRemoteObject implements RMIInterface {

    public RMIServerTest() throws RemoteException {
    }

    @Override
    public void helloWorld(int i) throws RemoteException {
            for (int j = 0; j < i; j++) {
                    System.out.println("Hello World");
            }
    }

    public static void main(String[] args) {
            try {
                    LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
            }

            catch (RemoteException ex) {
                    System.out.println(ex.getMessage());
            }
            try {
                    Naming.rebind("Server", new RMIServerTest());
            } catch (MalformedURLException ex) {
                    System.out.println(ex.getMessage());
            } catch (RemoteException ex) {
                    System.out.println(ex.getMessage());
            }

    }
}

和我们的客户:

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;


public class RMIClient {
  public static void main(String[] args) throws RemoteException, NotBoundException,MalformedURLException {
    try {

       RMIInterface serverObject = (RMIInterface) Naming.lookup("//externalServerAdress/Server");
        serverObject.helloWorld(10);
    }
    catch (Exception e) {
        System.out.println(e.getMessage());

    }


  }
}

我们仍然收到此错误:

Connection refused to host: 192.168.0.13; nested exception is: 
java.net.ConnectException: Connection timed out: connect

192.168.0.13 是其路由器后面的服务器的本地 IP 地址。我们使用路由器的外部 IP 在客户端连接。像“2.246.133.155”= externalServerAdress。 所以我们有联系。我们通过服务器的外部 IP 地址(WAN IP)连接,错误显示,它获取了服务器的本地 IP 地址,但仍然拒绝连接。

感谢任何提示。

【问题讨论】:

    标签: java rmi


    【解决方案1】:
    Connection refused to host: 192.168.0.13
    

    这不是互联网地址。这是一个仅存在于您的路由器后面的私有地址。您需要使用您的公共 IP 地址,并通过路由器安排端口转发。

    【讨论】:

    • 我们在客户端连接到服务器网络的路由器IP地址。我们真的不知道为什么我们会得到他路由器后面服务器的本地 IP 地址。但它不可能是魔法,所以存在联系。 (我编辑了原始帖子以使其更清晰)
    • 在这种情况下你需要阅读item A.1 of the RMI FAQ
    • 我们确实将外部 WAN IP 绑定到:System.setProperty("java.rmi.server.hostname", "xxx.xxx.xxx.xxx");但总是得到相同的错误(连接拒绝托管:192.168.0.13)有点奇怪:/
    • 您需要在创建任何远程对象(包括注册表)之前设置该属性。
    • 是我们路由器防火墙设置的问题 - 立即生效,我们自己的子网没有任何问题:)
    猜你喜欢
    • 2018-02-28
    • 2012-01-19
    • 2014-11-15
    • 1970-01-01
    • 2014-05-31
    • 2011-07-01
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多