我正在尝试在一台服务器和两个客户端之间建立双向通信。当所有程序都在同一台机器上运行时,这非常有效,但当我尝试使用 LAN 网络时它不起作用。我得到了错误:
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(String name) 代码大约是(在 Game 类中):
cd_client = (ClientInterface) Naming.lookup("rmi://127.0.0.1/" + name);
所有防火墙都被禁用。
我已经在这个问题上工作了好几个小时,但我仍然没有发现问题所在。如果您能帮我一点忙,我将不胜感激。谢谢