Java 本地主机上的RMI连接被拒绝

Java 本地主机上的RMI连接被拒绝,java,connection,localhost,rmi,Java,Connection,Localhost,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编码,当我运行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

这是我在stackoverflow上的其他问题中发现的。但对于为什么RMI需要这个代码才能正常工作,我也没有在网上找到任何解释。有人有解释吗?

Naming.rebind()的目标是RMI注册表。它必须在运行。它没有跑。所以你无法连接到它。因此,您得到了一个
连接异常

我相信实现的代码并不重要,因为运行代码的只是实现的接口


这既没有意义,也无关紧要。接口不“运行代码”。类中的方法运行代码,在本例中,实现类运行代码。然而,您的问题不在于“运行代码”,而在于连接到注册表。

2020年的问题是什么?它是RMI运行的特定端口还是网络连接之类的?(这对我来说是全新的)或者我可以放任何我想要的端口吗?我不是在复制代码,我在看youtube教程:P谢谢你的帮助
LocateRegistry.createRegistry(2020);