2011-12-01 2 views
14

Ho creato un tipo di client Server, Client con RMI. Ma ogni volta che eseguo il mio server dopo aver avviato rmiregistry dal prompt dei comandi, viene lanciato l'errore già in uso. È solo io che ho iniziato il rmiregistry. L'ho controllato da netstat.Porta di chiamata del metodo remoto in uso

Codice

Server:

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

    } 
} 

Codice Cliente:

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

Eccezione:

java.rmi.server.ExportException: Port already in use: 1099; nested exception is: 
java.net.BindException: Address already in use: JVM_Bind 

risposta

5

utilizza la porta 1099 nel suo processo, quindi non è possibile utilizzarla nella propria. O:

  1. Avviare il registro nello stesso processo, tramite LocateRegistry.createRegistry() (preferito).
  2. Esporta il tuo oggetto su una porta diversa.
  3. Avviare il rmiregistry su una porta diversa da quella 1099.
+0

che era il problema nel mio caso. L'ho capito e quando stavo per postare qui, ho trovato la tua risposta. netstat -aon mostra dove sta ascoltando. In questo caso il suo ascolto su tutte le interfacce. – Andrews

1

nuovamente controllare se la porta 1099 non viene utilizzato da qualsiasi altro processo o utente o avviare il rmiregistry su qualche altra porta.

6

Usa questo codice Server -

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); 
} 
+2

Perché meno di 55000? Anche la porta deve essere> 1024 per la maggior parte delle persone. Sarebbe molto meglio tentare prima la creazione, poi se fallisce, getRegistry)). Il tuo modo ha un problema con la finestra temporale. – EJP

+0

È possibile effettuare senza eccezioni? http://stackoverflow.com/q/14982760/897090 – Jofsey

+0

@Lescott Come ho detto nella domanda che hai citato, no. – EJP

39

Se si utilizza MacOS, ci si può fermare la porta seguente come:

Per prima cosa devi trovare il numero PID: lsof -i :1099

E poi uccidere quella porta: kill -9 PID_number

+0

fwiw -9 è fondamentalmente un 'force quit' – Shanimal

1

Utilizzare ps -aef | grep rmiregistry. trova il pid che usa rmiregistry. Kill the pid Den esegue nuovamente il server ... !!!!

0

Prova questo:

lsof -P | grep ':1099' | awk '{print $2}' | xargs kill -9