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

时间:2014-12-11 10:02:22

标签: java port rmi server

我试过使用RMI,这是服务器端。起初它没有任何异常,但现在经过三次尝试运行以下代码后,我会得到一些错误

代码是:

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

错误是这样的:我试图将Object1更改为例如Object2,但是我将再次收到错误,同时我也会更改端口号...

什么是解决方案?

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

4 个答案:

答案 0 :(得分:2)

java.net.ConnectException: Connection refused

此异常可能有几个原因:

  • 您尚未在后台启动rmiregistry
  • 您正在尝试连接错误的端口号。
  • 您的防火墙可能会阻止连接。

答案 1 :(得分:2)

答案非常简单,默认情况下,注册表在端口1099上运行。要在其他端口上启动注册表,请在命令行上指定端口号。不要忘记取消设置CLASSPATH环境变量。有关更多信息,请查看此链接:Running the Example Programs

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

注意:如果其他服务使用1099你必须测试1100,如果也使用1100,你可以使用1101,依此类推。 : - )

答案 2 :(得分:0)

您还没有启动RMI注册表。

当您通过此操作时,如果在调用远程方法时仍然发生这种情况,请参阅item A.1 of the RMI FAQ

答案 3 :(得分:0)

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

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