Re: How To get port of the RMI Server?

Peter Jones <[email protected]> Thu, 16 Apr 2009 14:46:13 -0400
Newsgroups gmane.comp.java.sun.rmi
Message-ID <20090416184613.GA18131@east>
On Thu, Apr 16, 2009 at 02:17:42AM -0600, Alex Co wrote:
> I just want to know who can i get the port that RMI server is
> listenning to.

The result of invoking toString() on the stub should include the port
number, but not in a specified format, so a programmatic dependency on
this would be non-portable and subject to breakage.

There is a portable, supported way to access the TCP port of a stub
programmtically, but it is rather convoluted: parse the serialized
form of the stub.  Here is a blog post with considerable detail about
doing that, both in general and actually for this particular case:

http://weblogs.java.net/blog/emcmanus/archive/2007/06/disassembling_s.html

If you just want to discover the port used by a remote object in a
given server JVM at runtime for debugging purposes, one way to do
that-- with Sun's JDK, at least-- is by analyzing a thread dump of the
JVM and looking for threads with the name "(RMI) TCP Accept-####"
(where #### is the port number).

Also, FWIW, note that you can control the port number to use at remote
object export time:

http://archives.java.sun.com/cgi-bin/wa?A2=ind0302&L=rmi-users&P=3663

-- Peter

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff RMI-USERS".  For general help, send email to
[email protected] and include in the body of the message "help".

For a list of frequently asked RMI questions please refer to:
http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html

To view past RMI-USERS postings, please see:
http://archives.java.sun.com/archives/rmi-users.html