与 localhost 的 Rmi 连接被拒绝

2022-01-25 00:00:00 connection localhost java rmi

我在使用 java rmi 时遇到问题:

当我尝试运行我的服务器时,我得到了一个 connectException(见下文).

执行rebind方法时发生异常:

Runtime.getRuntime().exec("rmiregistry 2020");我的服务器服务器 = 新的我的服务器();Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);

当使用 rmi://localhost:2020/RemoteDataPointHandler 代替时,它也不起作用.使用默认端口也不起作用.我也尝试过使用 127.0.0.1 的 IP 地址,但效果相同.

我的运行时参数:

-Djava.security.policy=java.security.AllPermission

<上一页>线程主"java.rmi.ConnectException 中的异常:连接拒绝主机:localhost;嵌套异常是:java.net.ConnectException:连接被拒绝在 sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)在 sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)在 sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)在 sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306)在 sun.rmi.registry.RegistryImpl_Stub.rebind(未知来源)在 java.rmi.Naming.rebind(Naming.java:160)在 be.fortega.knx.server.Main.(Main.java:25)在 be.fortega.knx.server.Main.main(Main.java:16)引起:java.net.ConnectException:连接被拒绝在 java.net.PlainSocketImpl.socketConnect(本机方法)在 java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)在 java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433)在 java.net.Socket.connect(Socket.java:524)在 java.net.Socket.connect(Socket.java:474)在 java.net.Socket.(Socket.java:371)在 java.net.Socket.(Socket.java:184)在 sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)在 sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)在 sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569)... 7 更多

解决方案

好像换了就可以了

Runtime.getRuntime().exec("rmiregistry 2020");

通过

LocateRegistry.createRegistry(2020);

有人知道为什么吗?有什么区别?

I have a problem using java rmi:

When I'm trying to run my server, I get a connectException (see below).

Exception happens when executing the rebind method:

Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);

when using rmi://localhost:2020/RemoteDataPointHandler instead, it doesn't work either. Also using the default port does not work. I also tried using the 127.0.0.1 ip-address, but with the same effect.

my runtime args:

-Djava.security.policy=java.security.AllPermission

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:160)
    at be.fortega.knx.server.Main.(Main.java:25)
    at be.fortega.knx.server.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433)
    at java.net.Socket.connect(Socket.java:524)
    at java.net.Socket.connect(Socket.java:474)
    at java.net.Socket.(Socket.java:371)
    at java.net.Socket.(Socket.java:184)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569)
    ... 7 more

解决方案

It seems to work when I replace the

Runtime.getRuntime().exec("rmiregistry 2020");

by

LocateRegistry.createRegistry(2020);

anyone an idea why? What's the difference?

相关文章