我正在尝试学习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);
我从*上的其他问题中找到了.但是没有解释,也没有在网上找到为什么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.
这既无意义又无关紧要.接口不会“运行代码”.类中的方法运行代码,在这种情况下,实现类运行代码.但是你的问题不是’运行[代码],而是连接到注册表.