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

RMI connection refused on localhost(本地主机上的 RMI 连接被拒绝)

本文介绍了本地主机上的 RMI 连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to learn RMI coding and when I run server side of RMI I get connection refused. 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);

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

which I found from other questions here on stackoverflow. But there was no explanation nor did I find one online as to why this code was needed for RMI to work properly. Does anyone have an explanation?

推荐答案

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

The target of Naming.rebind() is the RMI Registry. It has to be running. It wasn't running. So you couldn't connect to it. So you got a 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.

这篇关于本地主机上的 RMI 连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:本地主机上的 RMI 连接被拒绝

基础教程推荐