关于在两台linux上架设rmi出现的问题

在两台linux下架设rmi经常会出现以下异常,Naming.lookup(url)中url的ip地址明明是server端的ip,可返回的信息却是被本机的ip拒绝,确实很怪异,异常如下:

java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused

解决方法是在server端的代码中添加一行代码:

System.setProperty("java.rmi.server.hostname",server端的ip);//添加这行代码
LocateRegistry.createRegistry(1099);
RemoteImpl service = new RemoteImpl();
Naming.rebind("RmiServer", service);