我正试图在一台服务器和两台客户机之间建立双向通信。当所有程序在同一台计算机上运行时,这种方式非常有效,但当我尝试使用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