RMI: Can not connect Client on a custom port - Java

This is a discussion on RMI: Can not connect Client on a custom port - Java ; Hi all I have a problem with connecting client on a custom port (other then 1099) in my RMI application I've made the smallest possible test case which clarifies my problem. So my test consists of two classes (ServerImpl and ...

+ Reply to Thread
Results 1 to 3 of 3

RMI: Can not connect Client on a custom port

  1. Default RMI: Can not connect Client on a custom port

    Hi all I have a problem with connecting client on a custom port (other
    then 1099) in my RMI application

    I've made the smallest possible test case which clarifies my problem.
    So my test consists of two classes (ServerImpl and Client) and one
    interface (IServer). In the example i'm trying to establish connection
    on port 1100


    // IServer interface
    package rmi;

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

    public interface IServer extends Remote {
    public int sum(int a, int b) throws RemoteException;
    }


    // ServerImpl class
    package rmi;

    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;


    public class ServerImpl implements IServer {
    ServerImpl() throws RemoteException {
    super();
    }

    public int sum(int a, int b) throws RemoteException {
    return a + b;
    }

    public static void main(String args[]) throws RemoteException {
    IServer server = new ServerImpl();
    Registry registry = null;

    final int port = 1100;
    try {
    registry = LocateRegistry.createRegistry(port);
    } catch (RemoteException e) {
    throw new RemoteException(String.format(
    "Could not create registry on port: %d,"
    + " details have been logged", port));
    }
    Remote stub = UnicastRemoteObject.exportObject(server, port);

    /* Try to bind the remote object in registry */
    registry.rebind("rmi://127.0.0.1/MyServer", stub);
    }
    }



    // Client class
    package rmi;

    import java.rmi.Naming;

    public class Client {
    public static void main(String[] args) {
    String url = "rmi://127.0.0.1:1100/MyServer";
    IServer remoteObject;
    try {
    remoteObject = (IServer) Naming.lookup(url);

    System.err.println("Got remote object");

    System.err.println(" 1 + 2 = " + remoteObject.sum(1, 2));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }



    This example produces "java.rmi.NotBoundException: MyServer" I've
    tried various combinations of things set in url variable that resides
    in Client class with no luck. E.g. if I set it to String url =
    "MyServer:1100";

    I get "java.net.MalformedURLException: not a hierarchical URL:
    MyServer:1100"

    Right now I really do not know what else should I try... hope someone
    can help me with this

    Thanks,
    Roman

  2. Default Re: RMI: Can not connect Client on a custom port

    Register your code with rmi registry and start the server.

    Following link have all the step to run sample rmi server

    http://sumit-bhasin.talkmeblog.com/S...e-b1-p5536.htm

  3. Default Re: RMI: Can not connect Client on a custom port

    On 13 ΣΕΞΤ, 23:47, Royan <romayan...@gmail.com> wrote:
    > Hi all I have a problem with connecting client on a custom port (other
    > then 1099) in my RMI application
    >
    > I've made the smallest possible test case which clarifies my problem.
    > So my test consists of two classes (ServerImpl and Client) and one
    > interface (IServer). In the example i'm trying to establish connection
    > on port 1100
    >
    > // IServer interface
    > package rmi;
    >
    > import java.rmi.Remote;
    > import java.rmi.RemoteException;
    >
    > public interface IServer extends Remote {
    > š š public int sum(int a, int b) throws RemoteException;
    >
    > }
    >
    > // ServerImpl class
    > package rmi;
    >
    > import java.rmi.Remote;
    > import java.rmi.RemoteException;
    > import java.rmi.registry.LocateRegistry;
    > import java.rmi.registry.Registry;
    > import java.rmi.server.UnicastRemoteObject;
    >
    > public class ServerImpl implements IServer {
    > š š ServerImpl() throws RemoteException {
    > š š š š super();
    > š š }
    >
    > š š public int sum(int a, int b) throws RemoteException {
    > š š š š return a + b;
    > š š }
    >
    > š š public static void main(String args[]) throws RemoteException {
    > š š š š IServer server = new ServerImpl();
    > š š š š Registry registry = null;
    >
    > š š š š final int port = 1100;
    > š š š š try {
    > š š š š š š registry = LocateRegistry.createRegistry(port);
    > š š š š } catch (RemoteException e) {
    > š š š š š š throw new RemoteException(String.format(
    > š š š š š š š š š š "Could not create registry on port: %d,"
    > š š š š š š š š š š š š š š + " details have been logged", port));
    > š š š š }
    > š š š š Remote stub = UnicastRemoteObject.exportObject(server, port);
    >
    > š š š š /* Try to bind the remote object in registry */
    > š š š š registry.rebind("rmi://127.0.0.1/MyServer", stub);
    > š š }
    >
    > }
    >
    > // Client class
    > package rmi;
    >
    > import java.rmi.Naming;
    >
    > public class Client {
    > š š public static void main(String[] args) {
    > š š š š String url = "rmi://127.0.0.1:1100/MyServer";
    > š š š š IServer remoteObject;
    > š š š š try {
    > š š š š š š remoteObject = (IServer) Naming.lookup(url);
    >
    > š š š š š š System.err.println("Got remote object");
    >
    > š š š š š š System.err.println(" 1 + 2 = " + remoteObject.sum(1, 2));
    > š š š š } catch (Exception e) {
    > š š š š š š e.printStackTrace();
    > š š š š }
    > š š }
    >
    > }
    >
    > This example produces "java.rmi.NotBoundException: MyServer" I've
    > tried various combinations of things set in url variable that resides
    > in Client class with no luck. E.g. if I set it to šString url =
    > "MyServer:1100";
    >
    > I get "java.net.MalformedURLException: not a hierarchical URL:
    > MyServer:1100"
    >
    > Right now I really do not know what else should I try... hope someone
    > can help me with this
    >
    > Thanks,
    > Roman


    The correct answer to my question is
    registry = LocateRegistry.createRegistry(port); // for server
    registry.rebind("MyServer", stub); // for server


    Naming.lookup("rmi://myhost:1100/MyServer"); // for client

+ Reply to Thread