2013-04-04 79 views
0

我正試圖在一臺服務器和兩臺客戶機之間建立雙向通信。當所有程序在同一臺計算機上運行時,這種方式非常有效,但當我嘗試使用LAN網絡時,它不起作用。 我得到了錯誤:RMI-在同一臺機器上工作,不在局域網中

java.rmi.ConnectException: Connection refused to host: 192.168.1.24; nested exception is: 
java.net.ConnectException: Connection timed out: connect 

這裏是服務器代碼:

public class Server{ 
private Game partie; // The class Game extends UnicastRemoteObject and implements ServerInterface 


public Server() throws RemoteException { 
    System.setProperty("java.rmi.server.hostname", "192.168.1.24"); 
    partie = new Game(); 
    LocateRegistry.createRegistry(1099); 
    try{ 
     Naming.rebind("Server", partie); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 
public static void main(String argv[]) throws RemoteException{ 
    new Server(); 
} 
} 

下面是客戶端代碼的構造:

public Client(String aName, String aServerAdress) throws RemoteException { 
    super(); 
    name = aName; 
    ServerAdress = aServerAdress; // = "192.168.1.24" 
    theRegistry = LocateRegistry.getRegistry(ServerAdress); 

    try { 
     serverInterface = (ServerInterface) theRegistry.lookup("Server"); 
    } catch (NotBoundException e1) { 
     e1.printStackTrace(); 
    } 



    try { 
     theRegistry.bind(name, this); // For two-way communication 
    } catch (AlreadyBoundException e) { 
     e.printStackTrace(); 
    } 


    serverInterface.registerClient(name); 
} 

凡registerClient(字符串名稱)代碼大約(遊戲類中):

cd_client = (ClientInterface) Naming.lookup("rmi://127.0.0.1/" + name); 

所有防火牆都被禁用。

我一直在解決這個問題好幾個小時了,我還沒有找到問題所在。如果你能幫我一下,我會很感激。 謝謝

+0

本地網絡和局域網?你如何區分這些? – asgs 2013-04-04 18:18:50

+0

127.0.0.1&192.168.1.24不是(必然)是同一臺機器(就您的網絡代碼而言)。使你的參考一致。 – KevinDTimm 2013-04-04 18:20:58

+0

192.168.1.24是我用來運行服務器應用程序的機器的地址 – maxence5694 2013-04-04 18:27:11

回答

4

變化127.0.0.1的所有出現(除註冊表綁定)到您的局域網IP地址(192.168.1.24你的情況)

127.0.0.1是一個Loopback地址:

「環回(環回)描述了將電子信號,數字數據流或者來自其始發地址的設備的數據流路由回到源的接收端,而沒有故意的 處理或修改,這主要是一種測試手段 傳輸或運輸基礎設施。「 - 來自維基百科

+0

謝謝,這個工程。但現在我有另一個錯誤: java.rmi.ServerException:在服務器線程中發生RemoteException;嵌套的異常是: java.rmi.AccessException:Registry.Registry.rebind不允許;原產地/192.168.1.56是非本地主機'。 起源是客戶端代碼中的'theRegistry.bind(name,this)'。我可以在註冊表(它在服務器上)上註冊客戶端嗎?所以服務器可以調用客戶端上的方法。 – maxence5694 2013-04-04 19:01:26

+0

看着http://stackoverflow.com/questions/820719/using-single-rmi-registry它似乎你實際上應該使用回送註冊表綁定。編輯我的答案以及 – 2013-04-04 19:10:50

+0

我在registerClient方法中添加了客戶端註冊表綁定,它運行良好,感謝您的幫助。 – maxence5694 2013-04-04 19:41:13

相關問題