Hi Bess,
Please put code inside [ code] [ /code] blocks - it makes it much easier to read.
I assume that this was code that you typed directly into the browser window rather than copying and pasting your real code. As such I am ignoring errors that I consider to be just typos. This could mean that I might ignore a real error in your code.
//AM I START THIS IN RIGHT PLACE???
This should work fine.
With your call to createRegistry() and rebind() in the same try ... catch block, if the first fails then the second will never be called. This means that you will have to put in your instructions that your application
must start its own registry, and if a registry is already running on the default port then it must either be stopped or another port number must be specified. I think this is a valid design decision for the assignment, but it is probably not what you want in a real life application.
Question :when start server user only asked for port no here, for createRegistery, does it need host name?
if yes how createRegistry with a specifit hostname
You can only start a registry on the localhost. This is for security reasons (not to mention how problematic it would be to start a registry on a remote machine).
But ... see my comment further down about the hostname in the server code.
Question :when start client - user only asked for host name here, for Naming.lookup, does it need port no?
if yes where is it being use?
Yes, the client
may also need the port number, especially if the rmi registry has been started on a non standard port.
If you allow the rmi registry to be started on a different port then you
must allow the user to specify a port for the client application or it will not be able to connect to the rmi registry.
(i read the zhi's msg, got confuse, it seem like he asked user for port and host when start server, and i don't see the why hostname is needed when start server)
The hostname is not needed to start the RMI Registry, however it
could be used in the bind / rebind.
In theory, you could have the RMI Registry on machine A, the server on machine B, and the client on machine C. To do this, your server application needs to know the hostname (and port) to connect to the machine running the rmi registry. The URL is then coded the same way as per the Naming.lookup() call:
For security reasons, the RMI Registry that Sun provides will not allow you to bind from a remote host. So you might find it easier to leave out the hostname on the bind/rebind (since you will have a very hard time
testing remote registration) and simply stipulate in your instructions that the server must be on the same computer as the registry (if you stick with your concept that your server
must start the rmi registry then there is no reason to ever code in the hostname).
-----
Some other things for you to consider:
Consider using
java.rmi.registry.Registry.REGISTRY_PORT instead of the "magic number" 1099.
Why is your register() method static?
The rebind() call
silently replaces any existing RMI object currently bound. This is not very friendly, especially if you use a generic name like "account" to bind your code.
I think that you should try and use a more unique name to bind with, and possibly you should check if the name has already been bound (perhaps indicating that the server is already running?)
Regards, Andrew
[ June 08, 2003: Message edited by: Andrew Monkhouse ]