• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: RMI check available port or host number

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
i wanted to start RMI server without hardcode it in the class,
could anyone please let me know what class / method should i use to get a list of available port number and host name
thanks in advance
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bess
Welcome to JavaRanch.

could anyone please let me know what class / method should i use to get a list of available port number and host name


I do not think you should do this.
You cannot really know what ports are currently in use without doing some sort of port scan - most system administrators would not want you doing this. You could find out what ports are still available by trying to bind to each one, but this would take time, and could become out of date during the time you present the list to the user to choose one (and how are you going to display the list of available port numbers between 0 and 65535). Hostnames might not be determinable programattically - I have worked in companies where the only way to access a particular computer was to find out from the sys admin what the hostname and port was that you needed to access - the systems were designed to be invisible.
The way I did my assignment was to go with the defaults for everything, but allow the user to override them.
So I tried to start my RMI Registry on the default port. If that failed, I assumed that an RMI Registry was already running, and tried to bind to it. If that failed, I gave the user lots of information about what was wrong and how to fix it.
As I said though, the user could override ports and/or hostnames if they wanted to. They should not need to do this in order to run the applications, but the option was there.
Regards, Andrew
[ June 07, 2003: Message edited by: Andrew Monkhouse ]
 
Bess Moore
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much for reply, but i have more questions for u
1. when i start RMI -- java -jar runme.jar server
i start the below class. user will allow to enter the "port no" or default "1099" please tell me whether i am doing the right thing for createRegistry in here
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class RegAccount {
//DEFAULT RMI PORT
Private static int portNo = 1099;
public RegData() {}
public RegData(int port) {
this.register();
}
public static void register() {
try {
//AM I START THIS IN RIGHT PLACE??? java.rmi.registry.LocateRegistry.createRegistry(portNo);
AccountImpl accImp = new AccountImpl();
Naming.rebind("account", accImp);
} catch (Exception e) {}
}
}
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. when i start a client, client will asked for host name where i use to look up the server
(AccountHelper) Naming.lookup("rmi://"+hostname
+"/account");
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?
(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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thank you very much !!
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic