Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote method invocation port in use

I have created a Server, Client kind of program with RMI. But whenever I run my Server after starting the rmiregistry from command prompt, the port already in use error is thrown. Its only me who started the rmiregistry. I have checked it from netstat.

Server Code:

public class Server implements Runnable, Linker{


private static Server server = null;
    private static Linker l = null;
    private String name = null;
    public Server(){}

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;               
    }  
    public void run(){
        while(!("Andy").equalsIgnoreCase(name)){

        }
    }
    public static void createStub(){
        try{
            server = new Server();
            l = (Linker) UnicastRemoteObject.exportObject(server, 1099);

            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Link", l);
            System.out.println("Ready");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }                       
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        createStub();
        Thread t = new Thread(server);

    }
}

Client Code:

public class Client implements Runnable{


private Scanner sc = new Scanner(System.in);
    private Linker linker = null;

    public void loadStub(){
        try{
            Registry registry = LocateRegistry.getRegistry(1099);
            linker = (Linker) registry.lookup("Link");

        }catch(Exception e){

        }
    }
    public void run(){
        String ip = null;
        while(sc.hasNext()&&!(ip = sc.nextLine()).equalsIgnoreCase(":q")){
            try {
                linker.setName(ip);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    public static void main(String...args){
        Client client = new Client();
        client.loadStub();
        Thread t = new Thread(client);
        t.start();
    }
}

Exception:

java.rmi.server.ExportException: Port already in use: 1099; nested exception is: 
java.net.BindException: Address already in use: JVM_Bind
like image 439
Andrews Avatar asked Dec 01 '11 06:12

Andrews


People also ask

How do I open an RMI port?

By default, a random port is opened on the host system that is used by the RMI registry. You can control RMI ports where a firewall exists on a network and the exact control of which ports should be opened is necessary. You specify the properties in the $IMPACT_HOME/etc/ <ServerName> _server. props properties file.

How do I change my RMI port?

From the RMI Tutorial: By default, the registry runs on port 1099. To start the registry on a different port, specify the port number on the command line. Do not forget to unset your CLASSPATH environment variable.

What is Remote Method Invocation?

The Java Remote Method Invocation (RMI) application programming interface (API) enables client and server communications over the net. Typically, client programs send requests to a server program, and the server program responds to those requests. A common example is sharing a word processing program over a network.


2 Answers

If you're using macOS, you can stop port following as:

First thing you need to find the PID_number: lsof -i :1099

And then kill that port: kill -9 PID_number

like image 196
Tai Le Avatar answered Oct 20 '22 00:10

Tai Le


The rmiregistry is using port 1099 in its process so you can't use it in yours. Either:

  1. Start the registry in the same process, via LocateRegistry.createRegistry() (preferred).
  2. Export your object on a different port.
  3. Start the rmiregistry on a different port other than 1099.
like image 43
user207421 Avatar answered Oct 20 '22 00:10

user207421