java 为什么 RMI localhost 客户端使用无 localhost ip 来连接 RMI localhost 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4258016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:33:50  来源:igfitidea点击:

Why RMI localhost client uses a no localhost ip to connect the RMI localhost server

javaconnectionlocalhostrmi

提问by RMI-thinker

We are using a client/server RMI communication in the same computer (so all ip should be localhost).

我们在同一台计算机上使用客户端/服务器 RMI 通信(所以所有 ip 都应该是 localhost)。

We start the registry (using the default port 1099)

我们启动注册表(使用默认端口1099)

registry = LocateRegistry.createRegistry(port);

registry = LocateRegistry.createRegistry(port);

and export some objects to the RMI registry

并将一些对象导出到 RMI 注册表

Naming.rebind("//" + "localhost" + ":" + port + "/" + name, object);

Naming.rebind("//" + "localhost" + ":" + port + "/" + name, object);

From another process we retrieve some objects (remember everything runs in localhost)

我们从另一个进程中检索一些对象(记住一切都在 localhost 中运行)

MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);

MyRemoteObject ro = (MyRemoteObject) Naming.lookup("//" + "localhost" + ":" + port + "/" + name);

The issue happen when you run the app starting with local area network working and in the middle of the process you disable the network connection. If you run the app and the LAN is working no issue popup, and if you run the app and the LAN is not working no issue popup. It just happens when you change the LAN while you are running the app.

当您从局域网工作开始运行应用程序并在此过程中禁用网络连接时,会发生此问题。如果您运行应用程序并且 LAN 工作没有问题弹出窗口,如果您运行应用程序并且 LAN 没有工作没有问题弹出窗口。它只会在您运行应用程序时更改 LAN 时发生。

The exception thrown while executing Naming.lookup() method is this one:

执行 Naming.lookup() 方法时抛出的异常是这样的:

java.lang.RuntimeException: java.rmi.ConnectIOException: Exception creating connection to: 192.168.x.x; nested exception is: java.net.NoRouteToHostException: No route to host: connect

java.lang.RuntimeException: java.rmi.ConnectIOException: Exception creating connection to: 192.168.x.x; nested exception is: java.net.NoRouteToHostException: No route to host: connect

Debugging a little bit I found out that the

调试了一下,我发现

RemoteObject ($Proxy0) -> RemoteObjectInvocationHandler -> UnicastRef2 -> LiveRef -> TCPEndpoint

RemoteObject ($Proxy0) -> RemoteObjectInvocationHandler -> UnicastRef2 -> LiveRef -> TCPEndpoint

had the ip of the host (e.g.: 192.168.x.x) instead of "localhost" or 127.0.0.1 (what it would be what I wanted). And the isLocal boolean of liveRef object is always false.

拥有主机的 ip(例如:192.168.xx)而不是“localhost”或 127.0.0.1(这将是我想要的)。而 liveRef 对象的 isLocal 布尔值总是假的。

I don't know if it is clear enough. Sorry!!!

不知道说得够不够清楚。对不起!!!

Do you have any suggestions?

你有什么建议吗?

My Tries:

我的尝试:

I tried this solutions

我试过这个解决方案

  1. Run the jvm with -Djava.rmi.server.hostname=localhost argument
  2. Redefine RMIServerSocketFactory to return 127.0.0.1 everytime. (TCPEndpoint has 192.168.x.x ip and isLocal is always false)
  3. Call rebind and lookup with no host in the URI. This is supposed to mean localhost.
  1. 使用 -Djava.rmi.server.hostname=localhost 参数运行 jvm
  2. 重新定义 RMIServerSocketFactory 每次都返回 127.0.0.1。(TCPEndpoint 有 192.168.xx ip 并且 isLocal 总是假的)
  3. 在 URI 中没有主机的情况下调用重新绑定和查找。这应该意味着本地主机。

but none of these have worked.

但这些都没有奏效。

Any suggestion will be welcome.

欢迎任何建议。

采纳答案by Neeme Praks

java.rmi.server.hostnameshould do the trick. You did set it on server, right?

java.rmi.server.hostname应该做的伎俩。你是在服务器上设置的吧?

If java.rmi.server.hostname=localhostdoes not work, how about java.rmi.server.hostname=127.0.0.1or java.rmi.server.hostname=::1?

如果java.rmi.server.hostname=localhost不起作用,如何java.rmi.server.hostname=127.0.0.1java.rmi.server.hostname=::1

回答by RMI-thinker

Sorry, guys. My Mistake.

对不起大家。我的错。

As you point out I was setting the "java.rmi.server.hostname=localhost" argument to the clients processes. It runned like charm when I setted the argument to process which run RMI server.

正如您指出的那样,我正在为客户端进程设置“java.rmi.server.hostname=localhost”参数。当我将参数设置为运行 RMI 服务器的 process 时,它运行起来就像魅力一样。

Thanks a lot.

非常感谢。

回答by stacker

You could try to start your server with this Property set -Djava.rmi.server.hostname=localhost

您可以尝试使用此属性集启动服务器 -Djava.rmi.server.hostname=localhost

See also Section 3.5.4

另见第 3.5.4 节

This because the server binds to an interface, either 127.0.0.1 or 192.168.x.x

这是因为服务器绑定到一个接口,127.0.0.1 或 192.168.xx