2013-11-25 53 views
0

我有3個項目的Java應用程序。服務器,通用和客戶端。當我要使用Naming.lookup獲取存根時,會引發錯誤。這裏是異常如何修復java中的java.rmi.ConnectionException?

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秒後。

+0

運行客戶端之前,服務器是否運行? – Sionnach733

+0

是的。我運行服務器,然後運行客戶端 – user2999888

+0

您可以顯示每個運行的主要方法嗎?你有沒有在服務器代碼中的while循環,以便它保持運行? – Sionnach733

回答

-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) { } 
} 
} 
+0

Windows 7 64位 – user2999888

+0

這種錯誤必須在客戶端上。它無法連接到服務器的客戶端。 Te服務器運行正常嗎? –

+0

是的。它運行沒有錯誤。當我,我要去啓動客戶端時,我得到了那個錯誤。 – user2999888

0

註冊表被垃圾收集。使Registry參考爲靜態。