0

I've got problem with RMI comunication between 2 machines (win 7 and win xp VM). The exception with I have problem is:

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

It's really weired because during connection I use address 192.168.1.4 (server), but exception somehow show sth different. I disabled firewall on both side. Ping working to both side. I tried telnet to server and use server port: telnet 192.168.1.4 1099 and it's working... I can't figure out where the problem is. If I run this on host side (eg server side) everything works fine.

How is it look from 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

Client side is look like:

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);
    }

This connection is working fine if I run client on host side. If I use VM and try connect between 2 different machines, I can;t figure out what did I do bad

4

1 回答 1

0

There is something wrong with your etc/hosts file or your DNS setup. It is providing the wrong IP address to the server as the server's external IP address, so RMI embeds the wrong address into the stub, so the client attempts to connect to the wrong IP address.

If you can't fix that, you can set the system property java.rmi.server.hostname to the correct IP address at the server JVM, before exporting any RMI objects (including Registries). That tells RMI to embed that address in the stub.

于 2013-02-03T03:28:17.023 回答