Knowing the port number in RMI

Is there any way to find out the port number of both the client and the server during RMI?

When the result is returned to the RMI client, the next time the client requests the result, will the conversation between the client and the server be on the same port as the previous one, when the server was started from the moment the first result was returned, or is a new port created?

eg:

// call to a remote method add addServerIntf.add(d1,d2) // after this call i get the added result 

 // again call the add method by again starting the client addServerIntf.add(d1,d2) // I get the result back as usual 

In two different calls, does the client and server port number remain the same?

My client program exits after entering the java AddClient localhost 100 200 command. The method on the server returns 300 and the client exits. The next time I started my client again using java AddClient localhost 19 100. Now the request will be sent from the same port that was sent earlier, and will the server receive the request on the same port? Or is the situation different from what I just wrote?

+4
source share
3 answers

Is there any way to find out the port number of both the client and the server?

No. You cannot know the client port number in advance, since it is dynamically allocated over the connection, and the server’s IP port is built into the stub where you cannot get it. Why do you need to know? Information cannot bring you much benefit: you cannot use it.

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?

Or:

  • A client connection is reused with the same port number or
  • A new connection is created on the client with a possibly new local port.

It is impossible to say that. The server port remains fixed.

In two different calls, do the client and server port numbers remain the same?

See above.

My client program exits after entering a command like java AddClient localhost 100 200 . The method on the server returns 300, and the client exits. The next time I started my client again using java AddClient localhost 19 100 . Now the request will be sent from the same port that was sent earlier

Probably no.

and will the server receive a request on the same port?

It is impossible to say without seeing the server code. If you exported it to a fixed port, it is exported to a fixed port. If not, then no. Tautology really.

Or is the situation different from what I just wrote?

I do not understand the question.

+2
source

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.

+3
source

Know your TCP / IP: the client connects to the server on a specific port. The client port number is random (or it should be for security reasons), the server port number is known (otherwise the client will not be able to connect in the first place).

Then the TCP / IP protocol will establish a connection. The server will create a handler for the connection , and TCP / IP will assign a new random port to this connection . The server-side port number does not change. Since the server can use the client IP + port as a key to distinguish between different "sessions", any number of clients can connect to the same server port.

This means: after initializing the RMI structure, both the client and the server will talk to each other through a pair of ports that do not change while the connection exists.

If the connection is disconnected for any reason, you can establish a new connection, but it will probably get a new port number s on both sides of the client.

my client program completes

This means that the connection is disconnected. When you restart the client, a new client port is created. Most likely (at 65534: 65535, which is approximately equal to 1) there will be a new port number.

If you start the client again within two minutes, the port number s must be different, because the old port s is maintained for two minutes to ensure that β€œthe remote TCP received confirmation of its request to complete the connection ” (if you run netstat , you see them with TIME_WAIT status).

-1
source

Source: https://habr.com/ru/post/1387397/


All Articles