This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I have been working on my assignment for about 2 months now, and I am getting close to delivery time
This forum has really been a great help for me! This is the first time I post a message and the reason is that I am really stuck
The problem is my server rmi implementation. This is the code I am trying to use: java.rmi.registry.LocateRegistry.createRegistry(port); Naming.rebind("TestServer", c);
But this only works when I disable my network connection Otherwise I get this error message: "Connection refused to host: 192.168.0.102; nested exception is: java.net.ConnectException: Connection refused: connect"
This, however, works: Naming.rebind("rmi://localhost/TestServer", c);
The RMI reference implementation provided by Sun does not allow for the RMI registry to reside on one machine and the service to reside on another machine. If either the registry or the service were believing the other was remote due to network address translation this could cause the problem you are seeing.
There are ways around this, but I wonder whether it is necessary: you do, after all, have a workaround . But if you would like to know more, just ask.
From what I understand I am running NAT. Does this mean that this is not a problem when I deliver my assignment?
Do you know why this works? "Naming.rebind("rmi://localhost/TestServer", c);"
I suspect that when the RMI Registry is starting up, it is binding to the localhost. When your service tries to do a rebind without any server name, it does a generic call to the IP address of your computer which is getting translated. So the registry is rejecting it since the address of the service is not the same as the localhost address.
When you explicitly put localhost into the service's call to rebind, you are ensuring that the two addresses will match.
I thought that the first argument just was the name of the server and that it did not matter what you wrote there...
It is possible to tell the registry to only listen to calls on a particular IP address. This might be useful on a computer with multiple IP addresses - such as on a firewall or a DMZ. In such a case you would want to be able to specify in the call to rebind which registry you wish to bind to.
This is way beyond the requirements of 90% of RMI users (and completly out of scope for the SCJD assignment), so it is often not mentioned. You can find some information on this if you hunt for it, or get an advanced book on RMI. This JGuru page shows some of the ways around firewalls (which you can possibly extrapolate to see how it can be used in a multi-IP environment).