如何在java中修复java.rmi.ConnectException?

时间:2013-11-25 11:16:37

标签: java rmi

我有一个包含3个项目的Java应用程序。服务器,通用和客户端。当我要使用Naming.lookup获取存根时,会出现错误。这是例外

java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
java.net.ConnectException: Connection timed out: connect
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:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at com.simulator.connector.ServerConnector.<init>(ServerConnector.java:60)
at com.simulator.connector.ServerConnector.getServerConnector(ServerConnector.java:85)
at com.simulator.ui.tester.LoginUI.initServer(LoginUI.java:65)
at com.simulator.ui.tester.LoginUI.btLoginActionPerformed(LoginUI.java:268)
at com.simulator.ui.tester.LoginUI.access$200(LoginUI.java:40)
at com.simulator.ui.tester.LoginUI$3.actionPerformed(LoginUI.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at com.jtattoo.plaf.BaseButtonListener.mouseReleased(BaseButtonListener.java:60)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
   Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
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.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 48 more

服务器,

private void startServer() throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);
    registry.rebind("MyServer", new RemoteFactoryImpl());
}

客户端,

private ServerConnector() throws NotBoundException, MalformedURLException, RemoteException {       

    try {
        remoteFactory = (RemoteFactory) Naming.lookup("rmi://localhost:1099/MyServer");
    } catch (ConnectException ex) {
        ex.printStackTrace();
    } catch (UnknownHostException ex) {
        ex.printStackTrace();
    } catch (NotBoundException ex) {
        ex.printStackTrace();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (RemoteException ex) {
        ex.printStackTrace();

    }
}

当我运行此程序时,我在10秒后收到该错误。

2 个答案:

答案 0 :(得分:0)

注册表正在被垃圾收集。将Registry引用设为静态。

答案 1 :(得分:-1)

你在运行什么操作系统?它可以是DNS / IP地址查找。如果您运行服务器,请尝试

telnet localhost 1099

来自终端,是否连接?如果没有,您有DNS或防火墙问题。在这种情况下,请尝试

telnet 127.0.0.1 1099

telnet <insert your LAN IP address> 1099

并查看其中任何一项是否有效。至少这会告诉您问题是服务器还是客户端。

修改

正如Sionnach733建议的那样,您需要让服务器保持活力:

private void startServer() throws RemoteException {
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("MyServer", new RemoteFactoryImpl());
while(true) {
    try { 
        Thread.sleep(1000); 
    } catch(Exception e) { }
}
}