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

我正在尝试学习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");

}

我相信实现代码无关紧要,因为它只是运行代码的已实现接口。我得到的例外是这个

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

我相信实现代码无关紧要,因为它只是运行代码的已实现接口。

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

以上是 RMI连接在本地主机上被拒绝 的全部内容, 来源链接: utcz.com/qa/424447.html

回到顶部