gpt4 book ai didi

java - RMI 连接在本地主机上被拒绝

转载 作者:行者123 更新时间:2023-12-02 05:55:42 24 4
gpt4 key购买 nike

我正在尝试学习 RMI 编码,当我运行 RMI 的服务器端时,连接被拒绝。这是我的服务器主要方法

public static void main(String[] args)throws Exception {
Implementation impl=new Implementation();
Naming.rebind("//localhost:2020/RMI", impl);
System.out.println("Implementation has been bind to the name RMI and is ready for use");
}

我相信实现的代码并不重要,因为它只是将运行代码的实现接口(interface)。我得到的异常(exception)是这个

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; 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.rebind(Unknown Source)
at java.rmi.Naming.rebind(Unknown Source)
at epl362Project.Server.main(Server.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(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

我做错了什么?客户端和服务器端都将在我的笔记本电脑上运行,因为这只是一个练习。我是否遗漏了什么或者我的代码有问题?除了给我答案之外,请解释为什么会这样,因为我不仅仅是想让它发挥作用,我还在努力学习。

**编辑我发现为什么我的代码不起作用。我遗漏了一行代码:

LocateRegistry.createRegistry(2020);

这是我从 stackoverflow 上的其他问题中找到的。但没有任何解释,我也没有在网上找到解释为什么 RMI 需要这段代码才能正常工作。有谁解释一下吗?

最佳答案

Naming.rebind() 的目标是 RMI 注册表。它必须正在运行。它没有运行。所以你无法连接到它。所以你得到了一个ConnectException

I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code.

这既没有意义,也无关紧要。接口(interface)并不“运行代码”。类中的方法运行代码,在这种情况下,实现类运行代码。但是,您的问题不在于“运行代码”,而在于连接到注册表。

关于java - RMI 连接在本地主机上被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23104661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com