【问题标题】:RMI and 2 machines - two way communicationRMI 和 2 台机器 - 双向通信
【发布时间】:2013-02-03 00:11:46
【问题描述】:

我遇到了两台机器(win 7 和 win xp VM)之间的 RMI 通信问题。我有问题的例外是:

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

这真的很奇怪,因为在连接期间我使用地址 192.168.1.4(服务器),但异常以某种方式显示出不同的东西。我在两边都禁用了防火墙。平工作双方​​。我尝试 telnet 到服务器并使用服务器端口: telnet 192.168.1.4 1099 并且它正在工作......我无法弄清楚问题出在哪里。 如果我在主机端(例如服务器端)运行它,一切正常。

从 SERVER 看如何:

public class Server  
{
  public static void main(String args[]) 
  {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);

String portNum, registryURL;
try{       

  System.out.println("Enter the RMIregistry port number:");
  portNum = (br.readLine()).trim();
  int RMIPortNum = Integer.parseInt(portNum);
  startRegistry(RMIPortNum); // Registry registry = LocateRegistry.createRegistry(RMIPortNum);
  ServerSide_Impl exportedObj = new ServerSide_Impl();
  registryURL = "rmi://localhost:" + portNum + "/callback";
  //registryURL = "rmi://192.168.1.4:" + portNum + "/callback";

  Naming.rebind(registryURL, exportedObj);
  System.out.println("Callback Server ready.");

}// end try
catch (Exception re) {
  System.out.println(
    "Exception in HelloServer.main: " + re);
 } // end catch
 } // end main

//This method starts a RMI registry on the local host, if
//it does not already exists at the specified port number.
private static void startRegistry(int RMIPortNum) throws RemoteException
{
try 
{
  Registry registry = LocateRegistry.getRegistry(RMIPortNum);
  registry.list( );  
    // This call will throw an exception
    // if the registry does not already exist
}
catch (RemoteException e) 
{ 
  Registry registry = LocateRegistry.createRegistry(RMIPortNum);
}
} // end startRegistry

} // end class

客户端是这样的:

try
    {
        this.serverAd = serverAddress.getText();
        String path = System.getProperty("user.dir");
        String pathAfter = path.replace("\\", "/");
        String pathFile = "file:/"+pathAfter + "/wideopen.policy";

        System.setProperty("java.security.policy", pathFile);           
        System.setSecurityManager(new RMISecurityManager());

        this.hostName = hostNameTextField.getText();
        this.portNum = hostPortNumberTextField.getText();          
        RMIPort = Integer.parseInt(this.portNum);
        this.time = Integer.parseInt(timeTextField.getText());
        //this.registryURL = "rmi://localhost:" + this.portNum + "/callback";  
        String registryURLString = "rmi://"+this.serverAd+":" + this.portNum + "/callback"; 
        this.registryURL = registryURLString;
        ConsoleTextField.append("\n"+ this.registryURL + "\n");

        // find the remote object and cast it to an 
        // interface object     

        obj = (ServerSide_Interface)Naming.lookup(this.registryURL);
        boolean test = obj.Connect();
        if(test)
        {
            callbackObj = new ClientSide_Impl();
            // register for callback
            obj.registerForCallback(callbackObj);

            isConnected = true;

            ConsoleTextField.append("Nawiązano połaczenie z serwerem\n");
            TableModel modelTemp = obj.Server_GenerateStartValues();
            myDataTable.setModel(modelTemp);

            myDataTable.setEnabled(true);
        }
        else ConsoleTextField.append("Brak połączenia z serwerem\n");


    }
    catch (Exception ex ){
        ConsoleTextField.append(ex + "\n"); 
        System.out.println(ex);
    }

如果我在主机端运行客户端,此连接工作正常。如果我使用 VM 并尝试在 2 台不同的机器之间连接,我无法弄清楚我做错了什么

【问题讨论】:

    标签: rmi


    【解决方案1】:

    您的 etc/hosts 文件或 DNS 设置有问题。它向服务器提供了错误的 IP 地址作为服务器的外部 IP 地址,因此 RMI 将错误的地址嵌入到存根中,因此客户端尝试连接到错误的 IP 地址。

    如果您无法解决此问题,您可以在导出任何 RMI 对象(包括注册表)之前,在服务器 JVM 上将系统属性 java.rmi.server.hostname 设置为正确的 IP 地址。这告诉 RMI 将该地址嵌入到存根中。

    【讨论】:

    • 那么我应该如何正确设置这个属性呢?你能写出例子吗?
    • 真的需要一个如何设置系统属性的示例吗?
    • 呵呵..好的,我会设置它,如果有帮助,我会告诉你。谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多