dcsimg
RMI - Router - not working
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   M_R
Posted On:   Monday, February 11, 2008 11:56 AM

Hello everyone!!! I am stuck because I cannot get RMI working. - I've created a client program and a server program; - The server sets the remote object in rmi://192.168.1.7/Hello (Hello= remote interface) (local ip 192.168.1.7); - The client does connect to rmi://87.2.187.37/Hello (public ip, I can manually change it if necessary, 87.2.187.37); - RMI registry is started with LocateRegistry.createRegistry(1099); - on the same pc client and server work fine; - on 2 different machines, it is important to notice that if I start the client without starting the server, I get a NotBoundException, which is normal; but when I start b   More>>
			Hello everyone!!!
			
I am stuck because I cannot get RMI working.

- I've created a client program and a server program;

- The server sets the remote object in
rmi://192.168.1.7/Hello
(Hello= remote interface)
(local ip 192.168.1.7);

- The client does connect to
rmi://87.2.187.37/Hello
(public ip, I can manually change it if necessary, 87.2.187.37);

- RMI registry is started with LocateRegistry.createRegistry(1099);

- on the same pc client and server work fine;

- on 2 different machines, it is important to notice that if I start the client
without starting the server, I get a NotBoundException, which is normal; but when
I start both of them, I get
java.rmi.ConnectException: Connection refused to host: 192.168.1.7; nested
exception is:
java.net.ConnectException: Connection timed out: connect
which does mean that the connection would be possible, because the client sees the
server, but for some (for me) unexplicable reason, it doesn't connect.

- on both machines there are routers, with opened port 1099, correct port forwarding,
no firewall active, etc... No obstacles seem to be.

- I've read that RMI also uses an hidden port, in addition to port 1099, true┬ž? Could this
be related to my problem?

What can I do? Thanks for any advice!

CODE:
			
interface Hello extends Remote{
public String sayHello() throws RemoteException;
}

@SuppressWarnings("serial")
class HelloImpl extends UnicastRemoteObject implements Hello{
public HelloImpl() throws RemoteException{}
public String sayHello() throws RemoteException{
try {
return "Hello world! --> by " + RemoteServer.getClientHost();
} catch (ServerNotActiveException e) { e.printStackTrace(); }
return "OOK";
}
}

class HelloServer extends Thread{
public HelloServer(){ start();}
public void run(){
try{
HelloImpl ref= new HelloImpl();
String rmiObjName = "rmi://localhost/Hello";
Naming.rebind(rmiObjName,ref);
System.out.println("Server ready on --> "+rmiObjName+"
");
}catch(Exception e){ e.printStackTrace();}
}
}

class HelloClient extends Thread{
private String HOST = "public_ip_server"; //81.75.253.171 or 81.75.21.22 (www.myip.it)
public HelloClient(){start();}
public void run(){
try{
Hello ref= (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
System.out.println("Client call server: "+ ref.sayHello());
}catch(Exception e){ e.printStackTrace();}
}
}

//only in a server on "public_ip_server"
public class TestHelloServer {
public static final int P = 1099;
public static void main( String arg[]) throws Exception{
try {
java.rmi.registry.LocateRegistry.createRegistry(P);
} catch (RuntimeException e1) {}
Thread s1 = new HelloServer();
}
}

//only in a client
public class TestHelloClient {
public static final int P = 1099;
public static void main( String arg[]) throws Exception{
try {
java.rmi.registry.LocateRegistry.createRegistry(P);
} catch (RuntimeException e1) {}
Thread s2 = new HelloClient();
}
}
   <<Less

Re: RMI - Router - not working

Posted By:   Anonymous  
Posted On:   Tuesday, February 12, 2008 01:36 AM

Dear .....

I am not sure but you can check out one more process. that is:-


Run the policy tool on server and set all the permission to all permission. Always be sure to save your policy files in your home folder with name .java.policy. First give all the permission. If it works change the permission to required only. you can run it from command prompt by runnig the command policytool. If it works , plz inform me.


Shashi
"Java Is neither easy nor tough"

Re: RMI - Router - not working

Posted By:   Robert_Lybarger  
Posted On:   Monday, February 11, 2008 09:22 PM

The first thing is that I'm not sure you're code is quite right for standard RMI. (At least, not per the way Sun wrote the RMI trail in their java tutorial site.) So here's my version of what I think you were trying:

/* Hello interface same */

public class HelloImpl implements Hello {
public HelloImpl() {
super();
System.out.println("Hello object created.");
}

public String sayHello() throws RemoteException {
System.out.println(
"sayHello was called by remote client.");
return "OK";
}
}


public class Server {
public static void main(String[] args) {
Registry reg = null;
try {
reg = LocateRegistry.createRegistry(9099);
String myAddr =
InetAddress.getLocalHost().getHostAddress();
System.out.println(
"registry started on "+myAddr+":9099");
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
Hello helloObj = new HelloImpl();
Hello helloStub =
(Hello) UnicastRemoteObject.exportObject(helloObj,0);
String name = "hello";
reg.rebind(name, helloStub);
System.out.println("A hello object is ready.");
}
catch (Exception e) {
e.printStackTrace();
}
while (true) {
try { Thread.sleep(100); }
catch (Exception e) {}
}
}
}


public class Client {
public static void main(String[] args) {
try {
Registry reg =
LocateRegistry.getRegistry(args[0], 9099);
String name = "hello";
Hello helloObj = (Hello) reg.lookup(name);
String message = helloObj.sayHello();
System.out.println("The hello object said: "+message);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}


The Client class needs an argument to specify an address. I have a two-machine LAN which currently lacks local DNS. When I used a host name, I got a connect timeout. Using the IP fixed it for me. Things of note: the "client" doesn't need to start an rmiregistry... it just has to lookup and access the remote registry. Also, you were calling out to Naming instead of the Registry object returned by the LocateRegistry methods, and I'm not sure how you were getting a remote stub. (Then again, your construction of HelloImpl is also slightly different, so you might be trying a completely different approach, possibly using a much older RMI technique that has been simplified???)


So I definitely have the above code working on a two-machine LAN (using java 1.5) as well as in the same machine (in separate consoles). If this doesn't work for you, then double-check the IP addresses being used are right (you're on a floating address pool, it looks like; the hostname you referenced has a different IP now). Also, you seem to be throwing away some exceptions that, when things are not working, you should at least dump a stacktrace for ... but I imagine the issue is indeed somewhere in routing/addressing between the machines; sounds like you have a non-trivial configuration.

About | Sitemap | Contact