RMI: Can not connect Client on a custom port

Discussion in 'Java' started by Royan, Sep 13, 2008.

  1. Royan

    Royan Guest

    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
    Royan, Sep 13, 2008
    #1
    1. Advertising

  2. Royan

    Guest

    , Sep 14, 2008
    #2
    1. Advertising

  3. Royan

    Royan Guest

    On 13 ÓÅÎÔ, 23:47, Royan <> 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
    Royan, Sep 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Otis Mukinfus
    Replies:
    3
    Views:
    904
    Juan T. Llibre
    Jul 8, 2005
  2. Buu Nguyen

    RMI, JINI or RMI/IIOP

    Buu Nguyen, Aug 25, 2004, in forum: Java
    Replies:
    1
    Views:
    553
    Sudsy
    Aug 25, 2004
  3. JScoobyCed

    RMI Vs RMI over IIOP

    JScoobyCed, Jan 28, 2005, in forum: Java
    Replies:
    1
    Views:
    725
    Dag Sunde
    Jan 28, 2005
  4. Jan Schulze
    Replies:
    1
    Views:
    562
    Esmond Pitt
    Mar 26, 2005
  5. yellowtek

    RMI port on client side

    yellowtek, Aug 13, 2006, in forum: Java
    Replies:
    8
    Views:
    17,013
Loading...

Share This Page