Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...