简体   繁体   English

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

[英]RMI connection refused on localhost

I am trying to learn RMI coding and when I run server side of RMI I get connection refused. 我正在尝试学习RMI编码,当我运行RMI的服务器端时,连接被拒绝。 This is my server main method 这是我服务器的主要方法

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");
    }

I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code. 我认为,实现代码无关紧要,因为它只是运行代码的已实现接口。 The exception I am getting is this 我得到的例外是这个

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

What am I doing wrong? 我究竟做错了什么? both client and server sides will run on my laptop as this is just an exercise. 客户端和服务器端都将在我的笔记本电脑上运行,因为这只是一个练习。 Am I missing something or is there something wrong with my code? 我是否缺少代码或代码有问题? Please besides giving me the answer explain why it is so since I do not just want to make it work, I am trying to learn. 除了给我答案外,请解释为什么会这样,因为我不仅想让它起作用,而是在尝试学习。

**EDIT I found out why my code was not working. **编辑我发现了为什么我的代码无法正常工作。 I was missing a line of code that said 我缺少一行代码,说

LocateRegistry.createRegistry(2020);

which I found from other questions here on stackoverflow. 这是我在关于stackoverflow的其他问题中发现的。 But there was no explanation nor did I find one online as to why this code was needed for RMI to work properly. 但是没有任何解释,我也没有找到关于为什么RMI正常工作需要此代码的解释。 Does anyone have an explanation? 有人有解释吗?

The target of Naming.rebind() is the RMI Registry. Naming.rebind()的目标是RMI注册中心。 It has to be running. 它必须正在运行。 It wasn't running. 它没有运行。 So you couldn't connect to it. 因此,您无法连接到它。 So you got a ConnectException . 所以你得到了一个ConnectException

I believe that the code for Implementation does not matter as it simply is the implemented interface that will run the code. 我认为,实现代码无关紧要,因为它只是运行代码的已实现接口。

This is both meaningless and irrelevant. 这既没有意义,也没有意义。 Interfaces do not 'run the code'. 接口不会“运行代码”。 Methods in classes run the code, and in this case the implementation class runs the code. 类中的方法运行代码,在这种情况下,实现类运行代码。 However your problem is not in 'run[ning] the code', it is in connecting to the Registry. 但是,您的问题不在于“运行代码”,而在于连接注册表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM