This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I have been having a go at RMI between two PC's over my home LAN. The Linux box acts as a server, and hosts a simple Calculator Service. I have started the rmiregistry on the linux box with 'rmiregistry &', and then executed my server application, which binds the class as follows
System.out.println("Starting Calculator Server"); Calculator c = new CalculatorImpl(); System.out.println("\tRegister RMI Service"); Naming.rebind("rmi://localhost:1099/CalculatorService", c); System.out.println("\tRMI Service registered");
This seems to work, all the System.out... debug happens, and no exceptions.
Then on my client the XP machine, I try to connect as follows
Calculator c = (Calculator)Naming.lookup("rm://192.168.0.5/CalculatorService");
I am using the fixed IP address of the Linux box here.
Both machines have port 1099 open on their firewalls, but I get this exception on the client
I find this really curious because it looks like the client is trying to connect to itself. Suspecting that the localhst reference (127.0.0.1) is coming from the server binding, I changed localhost on the server to 192.168.0.5, and I get the same problem.
Can some throw some light on this please. I hoped this would be a straight forward 45 minuts exercise, and I ended up hacking for 2.5 hours!!
The problem is that you have only opened port 1099 - this is the port that the registry runs on - but your server runs on another port - which usually is randomly assigned. You can specify a port number in the UnicastRemoteObject constructor call in your server's constructor, or make a special RMIServerSocketFactory/RMIClientSocketFactory pair that uses a specified port (or range of ports). The first solution is probably a lot easier in your case, so specify a port in the super() call in your server's constructor, and open that port in your firewall, and you shouldn't get the exception.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Jul 02, 2006
Nate, Thanks for your reply.
My Server side code (running on Fedroa 5) has a class called CalculatorImpl, whose constructor calls super(1100);. From this I run rmic -vcompat CalculatorImpl, which gives me a stub and a skeleton. A second class called CalculatorServer is coded thus,
System.out.println("Starting Calculator Server"); Calculator c = new CalculatorImpl(); System.out.println("\tRegister RMI Service"); Naming.rebind("rmi://192.168.0.4:1099/CalculatorService", c); System.out.println("\tRMI Service registered");
Port 1099, and 1100 are both open on the Linux firewall.
I then run rmiregistry &, followed by java -Djava.rmi.server.hostname=192.168.0.4 CalculatorServer. I picked this up from a tip on another forum. CalculatorServer seems to start normally.
Then on my XP Pro machine I copy the CalculatorImpl_Stub.class file created earlier to the client directory, and execute my client code, which calls
Namin.lookup("rmi://184.108.40.206/CalculatorService");, and casts this to the interface type.
The firewall is open on 1099, and 1100.
I then get the afore mentioned exception.
I really do appreciate anyone's help, so thanks in advance.
Hmmmm... take a look at this post - it starts out talking about Jini - but further down the page mentions some information about how localhost and machine name specified in the /etc/hosts file can cause this error - you did mention that you had changed some host information around so you may already be doing this, though...
Also - check your network setup - make sure that you can ping the Linux server directly from the XP client, or access other services running on the Linux box, and check if the router is doing any kind of port forwarding to the XP box instead of the Linux box, etc.... if the XP client and Linux server are on opposite sides of the firewall - you may have to also specify the IP of the firewall in a proxy property and have the firewall do port forwarding to the Linux server.