0

I have a correction of an exercice we have done in class so its supposed to be working fine. But Im not able to execute it properly.

When I run the Remote object its all fine I have the message on the screen showing me its working but when I run the client afterwards I guedd this error :

**java.rmi.ConnectException: Connection refused to host: Sony-PC; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Unknown Source)
    at fr.unice.miage.m1.distributedsystems.tp3.client.Client.main(Client.java:16)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
    ... 7 more**

Im desperate, I tried everything. Maybe someone could tell me step by step how to get this to work.Thank you for your help !

Here s the Remote interface :

    import java.rmi.Remote;
    import java.rmi.RemoteException;

    public interface IRemote extends Remote {

    void echo() throws RemoteException;

    void echo(int value) throws RemoteException;

    String getRemoteAddress() throws RemoteException;

}

Remote Object CLASS :

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.AlreadyBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RemoteObject extends UnicastRemoteObject implements IRemote {

private static final long serialVersionUID = 1L;

protected RemoteObject() throws RemoteException {
    super();
}

@Override
public void echo() throws RemoteException {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("RemoteObject.echo()");
}

@Override
public void echo(int value) throws RemoteException {
    System.out.println("RemoteObject.echo() value=" + value);
}

@Override
public String getRemoteAddress() throws RemoteException {
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        return "Unknown";
    }
}

public static void main(String[] args) throws RemoteException,
        MalformedURLException, AlreadyBoundException {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }

    int rmiRegistryPort = 1099;

    // the rmiregistry can either be created programmatically or started
    // manually before each run if you have some time to waste
    Registry registry = LocateRegistry.createRegistry(rmiRegistryPort);
    System.out.println("RMI registry listening on port " + rmiRegistryPort);

    RemoteObject remoteObject = new RemoteObject();
    registry.bind("myremoteobject", remoteObject);
}

}

Client class

 import java.net.MalformedURLException;
    import java.net.UnknownHostException;
    import java.rmi.Naming;
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;

    import fr.unice.miage.m1.distributedsystems.tp3.server.IRemote;

    public class Client {

    public static void main(String[] args) {
        try {
            IRemote distante =
                    (IRemote)     Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost()+"/myremoteobject");
            System.out.println("Client.main() before echo remote call");
            distante.echo();
            System.out.println("Client.main() after echo remote call");

            distante.echo(7);
            System.out.println("Remote object IP: "
                    + distante.getRemoteAddress());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

    }
Mitch
  • 9
  • 5
  • Sounds like a firewall issue...or the RMI server is not running... – MadProgrammer Jan 10 '14 at 03:39
  • Please post the *entire* stack trace, and please clarify whether your client is running on a different host from your server. NB you don't need `java.net.InetAddress.getLocalHost()`. "localhost" will do. – user207421 Jan 10 '14 at 03:43
  • The firewall is off. I now when I tried to run it, I had another exception :/ just posted it above with my question – Mitch Jan 10 '14 at 04:11
  • Still waiting for the entire first stacktrace. The second one was caused by running with a security manager. It didn't just happen spontaneously. – user207421 Jan 10 '14 at 04:36
  • Use `IP address` instead of `localhost` – Pratik Butani Jan 10 '14 at 05:06
  • Please clarify whether your client is running on a different host from your server, which I asked for an hour ago, and please pay more attention to what you're asked here than you have so far. – user207421 Jan 10 '14 at 05:22
  • Sorry, its 6am in here im a little bit deconcentrated, no im running it on the same machine. Thats all the code I have, First I run Remote object class on eclipse, and then I run Client and I get that error. Maybe there is something Im not doing right ? Thats why I wanted someone to explain to me the steps to how Im supposed to be running it. For For the adress it used to be localhost and then I changed it to my acual local IP adress and then to java.net.InetAddress.getLocalHost() just to test and see if the problem comes from there but I still got the same error anyway. – Mitch Jan 10 '14 at 05:45
  • Sorry if Im maybe saying stupid things, RMI is all new to me I just started learning it today and I have to undrestand it to make my school project. – Mitch Jan 10 '14 at 05:50
  • Has the server JVM exited when you run your client? Can you ping `Sony-PC?` Telnet to it on port 1099? I suggest you make the Registry reference static in your server and try again. – user207421 Jan 10 '14 at 06:57
  • When I ping it its ok but when I telnet wth the port 1099 it says its impossible to open a connexion to the host I tried to make it static, still having the same problem... – Mitch Jan 10 '14 at 15:25
  • That's two answers to three questions. – user207421 Jan 11 '14 at 00:22
  • I dont undrestand what you mean by has the JVM exited when I run my client – Mitch Jan 11 '14 at 04:01

0 Answers0