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.
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.
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 callsuper(port)
with some non-zero port number.In the case where it doesn't extend
UnicastRemoteObject
, provide a non-zero port number toUnicastRemoteObject.exportObject()
.
There are several wrinkles to this.
If you aren't using socket factories, and you provide a non-zero port number when exporting your first remote object, RMI will automatically share that port with subsequently exported remote objects without specified port numbers, or specifying zero. That first remote object includes a Registry created with
LocateRegistry.createRegistry().
So if you create aRegistry
on port 1099, all other objects exported from that JVM can share port 1099.If you are using socket factories and your
RMIServerSocketFactory
has a sensible implementation ofequals()
, the same applies as above.Under both conditions, you can use the same non-zero explicit port number for all remote objects, e.g.
createRegistry(1099)
followed by any number ofsuper(1099)
orexportObject(..., 1099)
calls.
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 0 and 65535.
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
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)
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
The port is available here: java.rmi.registry.Registry.REGISTRY_PORT
(1099)
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 is1099
).
See more in the javadoc of java.rmi.registry.LocateRegistry
.
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.
来源:https://stackoverflow.com/questions/3071376/what-port-is-used-by-java-rmi-connection