My impression about RMI is as if they are different servers. I write an Interface as:
package pack;
import java.rmi.*;
public interface AddI extends Remote
{
public int add(int x, int y) throws Exception;
}
The class whose object to be served as:
package pack;
import java.rmi.server.*;
public class AddC extends UnicastRemoteObject implements AddI
{
public AddC() throws Exception
{
super();
}
public int add(int x, int y)
{
int i = 2;
while(i>0);
return x+y;
}
}
I want the add() method of above class to get stuck in while loop.
I write a Server program as:
package pack;
import java.rmi.*;
public class Server
{
public static void main(String[] args) throws Exception
{
AddC obj = new AddC();
Naming.rebind("rmi://10.100.11.238:1099/ADD", obj);
System.out.println("Server Started");
}
}
Then client program:
package pack;
import java.rmi.*;
public class Client
{
public static void main(String[] args) throws Exception
{
AddI obj = (AddI)Naming.lookup("rmi://10.100.11.238:1099/ADD");
int n = obj.add(5, 4);
System.out.println(n);
}
}
I started rmiregistry at port 1099 and run nmap command as:
$ nmap 127.0.0.1
Starting Nmap 7.01 ( https://nmap.org ) at 2016-11-24 15:14 IST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00011s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
21/tcp open ftp
80/tcp open http
1099/tcp open rmiregistry
I already have a web server running in my system. Then I run server program as:
$java pack.Server
Then after I run client in different machine as:
$java pack.Client
Since add() method has stuck in while loop so RMI server is not finished yet. In between if I run nmap again, it is not showing any extra open port.
Are both servers different or same?