RMI: Can not connect Client on a custom port

R

Royan

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
 
R

Royan

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top