Remote method invocation port in use
Asked Answered
D

6

15

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
Discipline answered 1/12, 2011 at 6:7 Comment(0)
I
7

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.
Isaacisaacs answered 1/12, 2011 at 8:15 Comment(1)
That was the problem in my case. I figured it out and when I was about to post here, I found your reply. netstat -aon shows where it is listening. In this case its listening on all interfaces.Discipline
P
48

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

Peraza answered 12/3, 2014 at 3:26 Comment(2)
fwiw -9 is basically a 'force quit'Nacreous
No. This will just kill the RMI Registry, which is needed. You have to find the underlying problem, not just shoot everything in sight.Isaacisaacs
I
7

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.
Isaacisaacs answered 1/12, 2011 at 8:15 Comment(1)
That was the problem in my case. I figured it out and when I was about to post here, I found your reply. netstat -aon shows where it is listening. In this case its listening on all interfaces.Discipline
A
7

Use this Server code -

Registry registry = null;
try {
    registry = LocateRegistry.getRegistry(52365);//use any no. less than 55000
    registry.list();
    // This call will throw an exception if the registry does not already exist
}
catch (RemoteException e) { 
    registry = LocateRegistry.createRegistry(52365);
}
Artilleryman answered 1/12, 2011 at 9:13 Comment(3)
Why less than 55000? The port also has to be > 1024 for most people. It would be much better to attempt the creation first, then if that fails do getRegistry)). Your way has a timing window problem.Isaacisaacs
Is it possible to make without exceptions? https://mcmap.net/q/821565/-is-it-possible-to-manually-check-locateregistry-existing/897090Relaxation
@Lescott As I said in the question you quoted, no.Isaacisaacs
H
0

Re-check if the port 1099 is not used by any other process or user or start the rmiregistry on some other port.

Hemipterous answered 1/12, 2011 at 6:22 Comment(0)
C
0

Use ps -aef |grep rmiregistry. find the pid which rmiregistry use. Kill the pid Den run the server again...!!!!

Calicut answered 5/7, 2014 at 11:34 Comment(0)
L
0

Try this:

lsof -P | grep ':1099' | awk '{print $2}' | xargs kill -9
Limited answered 28/12, 2017 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.