java.rmi.ConnectException:连接拒绝主机:127.0.0.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27420023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:20:55  来源:igfitidea点击:

java.rmi.ConnectException: Connection refused to host: 127.0.0.1

javaportrmiserver

提问by Elyas Hadizadeh

I've tried to used RMI, here is server side. at first it worked without any exception, but now after three times whenever i try to run the below code, i will get some errors

我试过使用 RMI,这里是服务器端。起初它毫无例外地工作,但现在每当我尝试运行下面的代码时,我都会得到一些错误

The code is:

代码是:

import java.rmi.server.UnicastRemoteObject;

/**
 * Created by elyas on 12/11/14 AD.
 */
public class LogicImplement extends UnicastRemoteObject implements Logic
{

    public LogicImplement() throws Exception
    {
        java.rmi.registry.LocateRegistry.createRegistry(6060);
        java.rmi.Naming.rebind("Object1",this);
    }

    @Override
    public int sum(int a, int b) throws Exception
    {
        int result = a + b;
        System.out.println("ana sum executed");
        return result;
    }

    public static void main(String[] args)
    {
        try
        {
            LogicImplement logicImplement  = new LogicImplement();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    } 
} 

The error is like this: i've tried to change the Object1 to for example Object2, but again i will get error, also i change the port number...

错误是这样的:我试图将 Object1 更改为例如 Object2,但我又会出错,我也更改了端口号...

what is solution?

什么是解决方案?

java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at LogicImplement.<init>(LogicImplement.java:12)
    at LogicImplement.main(LogicImplement.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 12 more

采纳答案by Elyas Hadizadeh

The Answer was very simple, By default, the registry runs on port 1099. To start the registry on a different port, specify the port number on the command line. Do not forget to unset your CLASSPATH environment variable. for more information check this link: Running the Example Programs

答案很简单,默认情况下,注册表在端口 1099 上运行。要在不同的端口上启动注册表,请在命令行中指定端口号。不要忘记取消设置您的 CLASSPATH 环境变量。有关更多信息,请查看此链接:运行示例程序

** So for fixing this code i must change the port number form 6060 to 1099

** 因此,为了修复此代码,我必须将端口号从 6060 更改为 1099

notice that: if 1099 is used by other services you have to test 1100, and if 1100 is used too, you have yo use 1101 and so on. :-)

请注意:如果1099被其他服务使用,你必须测试1100,如果1100也被使用,你必须使用1101等等。:-)

回答by Darshan Lila

java.net.ConnectException: Connection refused

There could be couple of reasons for this exception:

此异常可能有几个原因:

  • You have not started your rmiregistryin background.
  • You are trying to connect to the wrong port number.
  • Your firewall maybe blocking the connections.
  • 您还没有rmiregistry在后台启动。
  • 您正尝试连接到错误的端口号。
  • 您的防火墙可能会阻止连接。

回答by user207421

You haven't started the RMI Registry.

您尚未启动 RMI 注册表。

When you get past this, if it still happens when calling the remote method, see item A.1 of the RMI FAQ.

当您解决此问题后,如果在调用远程方法时仍然发生这种情况,请参阅RMI FAQ 的 A.1 项

回答by Dula

Since this was a port number issue, there is another way to start the registry on the port number you want:

由于这是端口号问题,因此还有另一种方法可以在您想要的端口号上启动注册表:

java.rmi.registry.Registry rmiRegistry = java.rmi.registry.LocateRegistry.
                createRegistry(6060); // Creates a registry on 6060
rmiRegistry.rebind("Object1",this);   // Binds to registry created on 6060