gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:31:28 26 4
gpt4 key购买 nike

我用 RMI 创建了一个服务器、客户端类型的程序。但是每当我从命令提示符启动 rmiregistry 后运行我的服务器时,就会抛出端口已在使用错误。只有我启动了 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);

}
}

客户端代码:

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

最佳答案

如果您使用的是 macOS,您可以停止端口跟随:

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

然后关闭那个端口:kill -9 PID_number

关于java - 正在使用的远程方法调用端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16698544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com