正在使用的远程方法调用端口

And*_*ews 15 java rmi exception

我用RMI创建了一个Server,Client类程序.但是每当我从命令提示符启动rmiregistry后运行我的服务器时,就会抛出已经处于使用状态的端口错误.只有我开始了这种情况.我从netstat检查了它.

服务器代码:

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);

    }
}
Run Code Online (Sandbox Code Playgroud)

客户代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

例外:

java.rmi.server.ExportException: Port already in use: 1099; nested exception is: 
java.net.BindException: Address already in use: JVM_Bind
Run Code Online (Sandbox Code Playgroud)

Tai*_* Le 42

如果您使用的是macOS,则可以将端口停止为:

首先需要找到PID_number: lsof -i :1099

然后杀掉那个端口: kill -9 PID_number


use*_*421 7

rmiregistry使用端口1099在其过程中,所以你不能在你使用它.或者:

  1. 通过LocateRegistry.createRegistry()(首选)在同一过程中启动注册表.
  2. 将对象导出到其他端口.
  3. rmiregistry在1099以外的其他端口上启动.


ani*_*ada 7

使用此服务器代码 -

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 为何不到55000?对于大多数人来说,端口也必须> 1024.首先尝试创建会好得多,如果失败则更好地执行getRegistry)).你的方式有一个时间窗问题. (2认同)