我已经创建了一个带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);
}
}
客户端代码:
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();
}
}
例外:
java.rmi.server.ExportException: Port already in use: 1099; nested exception is:
java.net.BindException: Address already in use: JVM_Bind
那是在我的情况的问题。我想清楚了,当我准备在这里发帖时,我发现了你的回复。 netstat -aon显示它正在侦听的位置。在这种情况下,它监听所有接口。 – Andrews