我正試圖在一臺服務器和兩臺客戶機之間建立雙向通信。當所有程序在同一臺計算機上運行時,這種方式非常有效,但當我嘗試使用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);
所有防火牆都被禁用。
我一直在解決這個問題好幾個小時了,我還沒有找到問題所在。如果你能幫我一下,我會很感激。 謝謝
本地網絡和局域網?你如何區分這些? – asgs 2013-04-04 18:18:50
127.0.0.1&192.168.1.24不是(必然)是同一臺機器(就您的網絡代碼而言)。使你的參考一致。 – KevinDTimm 2013-04-04 18:20:58
192.168.1.24是我用來運行服務器應用程序的機器的地址 – maxence5694 2013-04-04 18:27:11