Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Port Number while starting the server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Port Number while starting the server" Watch "Port Number while starting the server" New topic
Author

Port Number while starting the server

Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Ranchers
The RMI server i developed takes 2 command line arguments:
1) port no where the registry/server will listen the request
2) location of database file (db.db)
I am not asking for IP addrees of the machine
where this server is installed.
is it correct ?
Now the problem is as i am taking the port number as parameter and create the registry at the same port using the method createRegistry()
When i try to do rebind(name,obj)
This method requires a url formatted name that includes the hostname and port no. But the problem is i dont have the ip address of machine
where server is being tried to run? If dont give a URL formatted name then though it will bind the object in the registry located at local host but it will also take the default port which i dont want.
I tried to obtain IP address using InetAddress but it does not work.
Pls Help
Amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

You could just make it automatically take the default port number.
For the client, it will need to get the ip address of the server, if it is to connect to the Remote server.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Charles Miller
Greenhorn

Joined: Jun 28, 2002
Posts: 19
Just specify this for the URL...
rmi://localhost:XXXX/RemoteDataImpl
where XXXX is the port number that you want to use
and RemoteDataImpl is changed to whatever you want to register as the RMI name.
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Thanks Charles
Can i use InetAddress.getLocalHost().getHostAddress() to get the ip address of the
server machine.
Amit
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

Can i use InetAddress.getLocalHost().getHostAddress() to get the ip address of the
server machine.

That will get you the IP address of the local machine. The client is the one that needs the IP address of the Server. So if you get the local machines IP address you won't get the server, of course unless it is running on the same machine.
Mark
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Mark
Actually i was talking about using this at server only while binding the connection factory in registry.
Naming.rebind("rmi://"+InetAddress.getLocalHost().getHostAddress()":"+port +"/"+objConnectionFactory)
i am doing so as i am taking port no as command line argument from the user while starting the server.
Amit
Charles Miller
Greenhorn

Joined: Jun 28, 2002
Posts: 19
For the server when you are binding the object, you can specify 'localhost', you don't have to use the machine name or an IP.
When the client needs to make the remote connection, you will either need to specify the machine name (NOT localhost) or the IP of the server machine.
IE. when I start my server, I use 'localhost' in the URL string to bind the remote object.
When I start the client, I specify 192.168.1.210 as the server machine (since I use a local network).
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Thanks charles
Yours is also correct but still i dont find any harm in using InetAddress.getLocalHost().getHostAddress()
Both are correct

What do u say ?
Amit
Charles Miller
Greenhorn

Joined: Jun 28, 2002
Posts: 19
I prefer the 'localhost' way since it avoids 2 method calls (thereby improving performance), though either way gets the job done.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Naming.rebind("rmi://"+InetAddress.getLocalHost().getHostAddress()":"+port +"/"+objConnectionFactory)

What are you guys talking about? My rebind looks as simple as this, and it works:
rebind("Database Service", remoteDataFactory);
Am I missing anything?
Eugene.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17258
    
    6

No Eugene, yours is fine. Mine was that simple too, but there are some who want to give the user that starts things up more flexibility, even though you don't need it. But it is fine for them to have that flexibility, it is really just a preference.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Port Number while starting the server