What port is used by Java RMI connection?
Asked Answered
P

9

27

May I know what port is used by Java RMI connection?

If I want to connect a Java client application to a Java server application using RMI connection, what port I need to open at the server machine so that the client application can connect to it?

I want to set up a firewall in the server machine but I don't know which port I should open.

Plutocrat answered 18/6, 2010 at 16:20 Comment(0)
F
28

RMI generally won't work over a firewall, since it uses unpredictable ports (it starts off on 1099, and then runs off with a random port after that).

In these situations, you generally need to resort to tunnelling RMI over HTTP, which is described well here.

Finger answered 18/6, 2010 at 16:34 Comment(12)
Thank you very much! Have you read the answer from El Guapo at above? Do you think that is a workable alternative solution? Can I set the port at the server to a fixed port using the rmiregistry command?Plutocrat
@kwc: You can set the RMI registry to a fixed port, yes, but I don't believe you can control individual client-server connections.Finger
Time has overtaken this anser a little, if you are on Java 7 update 4 there is hope with the flag `` -Dcom.sun.management.jmxremote.rmi.port=7091`` See this blog post: hirt.se/blog/?p=289Kwakiutl
Even before java7 you can configure all rmi ports by using the (client/server)socketfactories. For details take a peek at rmi source code.Symonds
I am able to use 7474/browser/ and 7474/admin/Sleeper
@gian1200 The link should never have been posted in the first place. HTTP tunnelling is described perfectly adequately in the official RMI documentation. No need for arbitrary third-party references. However it is not required to solve this problem: see my answer.Prevail
@Symonds Even before Java 7 you can configure all RMI ports without using socket factories. For details take a peek at the RMI Javadoc.Prevail
ATM I see only mention of port 1099 in the answers but in this link it says the activation port is 1098. Is there more than one port used for RMI or has it changed? docs.oracle.com/javase/8/docs/technotes/guides/rmi/…Autotruck
@Autotruck The Registry uses 1099, the Activation Daemon uses 1098, and remote objects can use any port you like subject to RMI's port-sharing rules. By default it uses dynamic ports but this is easily over-ridden, which makes this answer and several of the comments incorrect.Prevail
@davey That property is for JMX, not RMI as a whole.Prevail
What? I don't believe this. I've been using static rmi port for years and that's the only port accepting connections, the rest are blocked by UFW. I've had no problem with that. Edit: user207421 answer makes sense why that is.Scully
@user207421: Hi could you please help with my question posted here.#67297660 . As after reading through all your comments here I feel like you can help me getting clear of my confusions.Shepard
P
27

All the answers so far are incorrect. The Registry normally uses port 1099, but you can change it. But that's not the end of the story. Remote objects also use ports, and not necessarily 1099.

If you don't specify a port when exporting, RMI uses a random port. The solution is therefore to specify a port number when exporting. And this is a port that needs opening in the firewall, if any.

  • In the case where your remote object extends UnicastRemoteObject, have its constructor call super(port) with some non-zero port number.

  • In the case where it doesn't extend UnicastRemoteObject, provide a non-zero port number to UnicastRemoteObject.exportObject().

There are several wrinkles to this.

  • If you are using socket factories, your RMIServerSocketFactory must have a sensible implementation of equals() for port sharing to work, i.e. one that doesn't just rely on object identity via == or Object.equals().

  • If either you don't provide a server socket factory, or you do provide one with a sensible equals() method, but not both, you can use the same non-zero explicit port number for all remote objects, e.g. createRegistry(1099) followed by any number of super(1099) or exportObject(..., 1099) calls.

Prevail answered 13/2, 2017 at 0:31 Comment(7)
This makes sense.Scully
I now find with Java 8 that the third bullet point above stating that 'RMI will automatically share that port with subsequently exported remote objects without specified port numbers' no longer appears to be true. I am still investigating this and will report further if necessary.Prevail
I've investigated this further and I find that either Oracle have broken it or I've been wrong for 20 years. Answer amended accordingly.Prevail
@greengold Eh? If you're exporting a client-side callback on port 1099 you need to open that port in the client's firewall.Prevail
And as for your claim there is no information, I've been providing answers and comments on this issue here for ten years, and for another ten years before that on the Oracle forums. There is plenty about it right here on this site.Prevail
If the client and server rpc use random port, then how do they know each other's port to connect to? Or they communicate through registry?Redbug
@MinhNghĩa The Registry doesn't use a random port, and the port of the remote object is embedded in the stub which is bound to the Registry. A TCP client doesn't need a fixed port number.Prevail
B
9

In RMI, with regards to ports there are two distinct mechanisms involved:

  1. By default, the RMI Registry uses port 1099

  2. Client and server (stubs, remote objects) communicate over random ports unless a fixed port has been specified when exporting a remote object. The communcation is started via a socket factory which uses 0 as starting port, which means use any port that's available between 1 and 65535.

Bureaucratize answered 18/6, 2010 at 16:21 Comment(6)
But I heard from somewhere that some Java application connections are set to use a random port. Do you have any idea what kind of Java connection is that? Does it related to Java RMI? Thanks.Plutocrat
Yes and no, see #1707185 for more detail on this.Bureaucratize
By default, the RMI Registry uses port 1099. By default, RMI remote objects use a random system-allocated port, unless a fixed port has been specified when exporting a remote object.Prevail
Only partly. You ignored the last twelve words a of my comment.Prevail
@EJP: thank you. I added the last part of your comment to the answer.Bureaucratize
And in the process you introduced another error. The port is in the range 1-65535, not 0-65535.Prevail
A
3

You typically set the port at the server using the rmiregistry command. You can set the port on the command line, or it will default to 1099

Accession answered 18/6, 2010 at 16:23 Comment(2)
it's probably best to send you the link to the sun page with it: java.sun.com/docs/books/tutorial/rmi/running.html if it's a linux box just make sure you have the $JAVA_HOME/bin in your path and you'll be able to run the commandAccession
This only applies to the Registry. It's not the end of the story.Prevail
B
2

If you can modify the client, then have it print out the remote reference and you will see what port it's using. E.g.

ServerApi server = (ServerApi) registry.lookup(ServerApi.RMI_NAME);
System.out.println("Got server handle " + server);

will produce something like:

Got server handle Proxy[ServerApi,RemoteObjectInvocationHandler[UnicastRef [liveRef: [endpoint:172.17.3.190:9001,objID:[-7c63fea8:...

where you can see the port is 9001. If the remote class is not specifying the port, then it will change across reboots. If you want to use a fixed port then you need to make sure the remote class constructor does something like:

super(rmiPort)
Benedikta answered 10/4, 2013 at 22:14 Comment(0)
U
0

Depends how you implement the RMI, you can set the registry port (registry is a "unique point of services"). If you don't set a explicit port, the registry will assume the port 1099 by default. In some cases, you have a firewall, and the firewall don't allow your rmi-client to see the stubs and objects behind the registry, because this things are running in randomically port, a different port that the registry use, and this port is blocked by firewall - of course. If you use RmiServiceExporter to configure your RmiServer, you can use the method rmiServiceExporter.setServicePort(port) to fixed the rmi port, and open this port in the firewall.

Edit: I resolve this issue with this post: http://www.mscharhag.com/java/java-rmi-things-to-remember

Unmanly answered 6/9, 2016 at 23:45 Comment(3)
The blog you cited completely ignores the existence of the port parameter to UnicastRemoteObject.UnicastRemoteObject() constructor and UnicastRemoteObject.exportObject(). The RmiServiceExporter is part of Spring, not Java RMI.Prevail
@EJP, I answered the central question about this post and COMPLEMENT my answer explaining a solution to change the service port with Spring. As can you see, my phrase started with "If you use RmiServiceExporter to ...". Thanks for your helpful response.Unmanly
You answered one of several questions, and introduced Spring without saying so and without it being mentioned in the question.Prevail
C
-1

From the javadoc of java.rmi.registry.Registry

Therefore, a registry's remote object implementation is typically exported with a well-known address, such as with a well-known ObjID and TCP port number (default is 1099).

See more in the javadoc of java.rmi.registry.LocateRegistry.

Carpel answered 18/2, 2015 at 22:22 Comment(1)
This only applies to the Registry. It's not the end of the story.Prevail
R
-1

The port is available here: java.rmi.registry.Registry.REGISTRY_PORT (1099)

Resuscitator answered 16/11, 2015 at 9:51 Comment(1)
The default port for the Registry is available here. That's not the end of the story.Prevail
C
-1

With reference to other answers above, here is my view -

there are ports involved on both client and server side.

  • for server/remote side, if you export the object without providing a port , remote object would use a random port to listen.

  • a client, when looks up the remote object, it would always use a random port on its side and will connect to the remote object port as listed above.

Carbonyl answered 9/10, 2017 at 12:11 Comment(4)
will there be any problem if the RMI is hosted on a random port (say 50000) ?Entry
nope, you can use any port to host the registry as long as it is available.Carbonyl
@Entry 'RMI is hosted on a random port' is meaningless. You should use 1099 for the RMI Registry. That's what it's for.Prevail
@Prevail 1099 is default port but its not the only port you can use. You can override and still access registry.Carbonyl

© 2022 - 2024 — McMap. All rights reserved.