Is there any way to find out the port number of both the client and the server during RMI?
If I did not understand, I think that there is no way to find out the port number during an RMI session.
If you have a different port number, from the default port of the RMI registry server 1099, you must set it in the Server class and in the Client class, because, like Oracle RMI Reports :
If the registry will work on a port other than 1099, you will need to specify the port number in calls to LocateRegistry.getRegistry in the Server and Client classes. For example, if the registry is running on port 2001 in this example, the getRegistry call in the server would be:
Registry registry = LocateRegistry.getRegistry(2001);
and
This client first obtains a stub for the registry by calling the static LocateRegistry.getRegistry method with the specified host name on the command line. If no host name is specified, null is used as the host name indicating that the local host address should be used.
Then I came to the conclusion that you cannot know, using the method, the port number of the RMI session (you can check the RMI API for details), unless you must install it if it is different from the default server port 1099 RMI server because you must know it at the beginning of an RMI session.
Think about it: how can you get this port number? How to contact the server or client? For example, if you request a page located on a specific Server that listens on port 81 (and not port 80 by default), you need to specify the port number in advance to connect to this specific Server by contacting it, for example: http://192.168.1.1:81 . Then, during the RMI session, you must know the RMI registry port in advance.
See page for details.
When the result is returned to the RMI client, the next time the client asks for the result, will the conversation between the client and the server be in the same port as the previous one, when the server has been running since the first result was returned or a new port was created?
When the result is returned to the client, the conversation between the client and the server must be divided into the same RMI registry port, otherwise, if the RMI registry installed on the client is different from the server's RMI registry port (if I have not forgotten), the code will throw RemoteException . which can happen when a failure occurs in the RMI process.
UPDATE
Now I see your updated question.
In two different calls, do the client and server port numbers remain the same?
It must be the same port on the RMI registry server. When your program exits after the first call, the program closes the socket connection. The next time you run 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 will get an unexpected result. If I understand, your client program simply calls the 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.