Is there a method to know the port number of both the client and
server during RMI?
If I didn't misunderstand, I think there's no way to know the port number during a RMI session.
If you have a different port number, from the default RMI registry Server 1099 port, you have to set it, on the Server class and on the Client class, because, as the Oracle RMI page reports:
If the registry will be running on a port other than 1099, you'll need
to specify the port number in the calls to LocateRegistry.getRegistry
in the Server and Client classes. For example, if the registry is
running on port 2001 in this example, the call to getRegistry
in the
server would be:
Registry registry = LocateRegistry.getRegistry(2001);
and:
This client first obtains the stub for the registry by invoking the
static LocateRegistry.getRegistry method with the hostname specified
on the command line. If no hostname is specified, then null is used as
the hostname indicating that the local host address should be used.
Then, my conclusion is that you can't know, with a method, the port number of a RMI session (you can check the RMI API for details), except when you have to set it, if it is different from the default RMI registry Server 1099 port, because you have to know it at the beginning of the RMI session.
Just think: How you can get this port number? Contacting the server or the client? For example, if you request a page located in a specific Server, that is listening to port 81 (not on the default 80 port), you need in advance the port number to connect to this specific Server, contacting it, for example on: http://192.168.1.1:81
. Then, during a RMI session you must know in advance the RMI registry port.
Check this page for details.
When a result is returned during RMI to the client, next time the
client requests the result, will the conversation between client and
server be at the same port as the previous, when the server has been
running since the first result was returned or a new port is created?
When the result is returned to the client, the conversation between Client and Server should be shared on the same RMI registry port, otherwise, if the RMI registry port set in the client is different from the Server RMI registry port (if I didn't forgot), the code will throw a RemoteException
, that can occur when a failure occurs in the RMI process.
UPDATE
Now I see your updated question.
In the 2 different calls does the port number of client and server
remain the same ?
It should be the same RMI registry Server port. When your program exits after the first call, the program closes the socket connection. The next time you start the program, the RMI registry port should be the same. Otherwise, your program should throw an exception, or, when you pass your arguments to the program, you get an unexpected result. If I understood, your client program simply calls a sum method on the Server. After the first result, the next time you start the program, do you get a different result? If not, I think the RMI registry port is the same.