java.rmi.ConnectException:当 RMI 服务器是 RMI 客户端的子进程时,连接被拒绝托管

发布于 2024-12-12 05:25:19 字数 843 浏览 0 评论 0原文

我有两个 Java 程序 - RMIServer 和 RMIClient。如果我将它们作为两个单独的 Java 调用启动,一切都会按预期工作。那就是

$ java -cp someclasspath server.mainserverclass &

$ java -cp someclasspath client.mainclientclass

但是在我的项目中,碰巧我需要客户端java程序来生成服务器然后连接到它。我使用了 runtime.exec() 方法和 ProcessBuilder 来生成服务器实例,但是当我这样做时,我得到以下结果:

.Exception in thread "main" java.rmi.ConnectException:  
    Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:

但有趣的是,如果手动终止 RMIClient 实例,RMIServer成为孤立的(父级是 pid:1),然后如果我运行 RMIClient,我可以很好地连接到孤立的 RMI 服务器!

因此,当 RMIServer 和 RMIClient 具有父子关系时,我无法通过 RMI 连接。

非常感谢任何帮助/指示。

*****回答*****(显然我没有足够的声誉来回答我自己的问题:-)

这是我的错误。 RMIServer 和 RMIClient 类在两个单独的 Java 线程中实例化。 RMIClient 类在 RMIServer 注册远程对象之前实例化,因此出现此异常。

亲子关系与这个问题无关。碰巧这些类的创建顺序是问题的根本原因。

I have two Java programs - RMIServer and RMIClient. Everything just works as expected, if I launch them as two separate Java invocations. That is

$ java -cp someclasspath server.mainserverclass &

$ java -cp someclasspath client.mainclientclass

But in my project, it so happens that I need the client java program to spawn the server and then connect to it. I used both the runtime.exec() method and ProcessBuilder to spawn a server instance, but when I do that I get the following:

.Exception in thread "main" java.rmi.ConnectException:  
    Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:

But the interesting part is that if manually kill the RMIClient instance, the RMIServer gets orphaned(parent is pid:1), and then if I run the RMIClient, I can connect to orphaned RMI server just fine!

So, in seems like when the RMIServer and RMIClient have a parent-child relationship, I am unable to connect via RMI.

Any help/pointers is much appreciated.

*****ANSWER***** (Apparently I do not have enough reputation to answer my own question :-)

This was my mistake. The RMIServer and RMIClient classes were instantiated in two separate Java threads. The RMIClient class for instantiated before the RMIServer registered the remote object and hence I get this exception.

The parent child relationship is irrelevant to this problem. It so happened that order in which these classes were getting created was the root cause of the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-12-19 05:25:19

碰巧我需要客户端java程序来生成服务器
然后连接到它

为什么?在这种情况下,您甚至不需要 RMI,更不用说第二个 JVM 了。只需在当前JVM中创建远程对象并直接调用其方法即可。

RMI 本身完全不知道父/子关系,所以我不会进行这种调查。异常字符串“连接拒绝托管:0:0:0:0:0:0:0:1”非常可疑。您从哪里获得远程对象的存根?这就是连接目标的来源。

it so happens that I need the client java program to spawn the server
and then connect to it

Why? In that circumstance you don't even need RMI let alone the second JVM. Just create the remote object in the current JVM and call its methods directly.

RMI itself is completely unaware of the parent/child relationship so I wouldn't pursue that line of investigation. The exception string 'Connection refused to host: 0:0:0:0:0:0:0:1' is extremely suspicious. Where did you get the stub for the remote object? That's where the connect target comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文