Run RMI client error message

476 views Asked by At

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

    }
0

There are 0 answers