You are on page 1of 3

/* * To change this template, choose Tools * and open the template in the editor. */ package rmicliente; import java.io.

*; import java.rmi.*; /** * * @author Allan Flores Rojas */ public class RMICliente {

Templates

/** * @param args the command line arguments */ public static void main(String[] args) { try{ int numPuertoRMI; String nombreNodo; InputStreamReader ent=new InputStreamReader(System.in); BufferedReader buf=new BufferedReader(ent); //System.out.println("Introducir el nombre del nodo del registro RMI :"); //nombreNodo=buf.readLine(); System.out.println("Introducir el nmero de puerto del registro RMI:") ; String numPuerto=buf.readLine(); numPuertoRMI=Integer.parseInt(numPuerto); String URLRegistro="rmi://localhost:"+numPuerto+"/holaMundo"; HolamundoInt h= (HolamundoInt)Naming.lookup(URLRegistro); System.out.println("Bsqueda completa"); String mensaje=h.decirHola("Pato Donald"); System.out.println("HolaMundoCliente: "+mensaje); } catch (Exception e){ System.out.println("Excepcin en Cliente: "+e); } } } ------------------------------------------------------------------------/* * To change this template, choose Tools Templates * and open the template in the editor. */ package rmiservidor; import java.rmi.*; import java.rmi.server.*; /** * * @author Allan Flores Rojas */ public class HolaMundoImpl extends UnicastRemoteObject implements HolamundoInt{ public HolaMundoImpl()throws RemoteException{ super(); }

public String decirHola(String nombre) throws RemoteException{ return "Hola mundo"+nombre; } } ------------------------------------------------------------------------/* * To change this template, choose Tools Templates * and open the template in the editor. */ package rmiservidor; import java.rmi.*; /** * * @author Allan Flores Rojas */ public interface HolamundoInt extends Remote{ public String decirHola(String nombre) throws java.rmi.RemoteException; } --------------------------------------------------------------------------/* * To change this template, choose Tools * and open the template in the editor. */ package rmiservidor; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.net.*; import java.io.*; /** * * @author Allan Flores Rojas */ public class RMIServidor { Templates

/** * @param args the command line arguments */ public static void main(String[] args) { InputStreamReader ent=new InputStreamReader(System.in); BufferedReader buf=new BufferedReader(ent); String numPuerto, URLRegistro; try{ System.out.println("Introducir el nmero de puerto del registro RMI: ") ; numPuerto=buf.readLine().trim(); int numPuertoRMI=Integer.parseInt(numPuerto); arrancarRegistro(numPuertoRMI); HolaMundoImpl objExportado=new HolaMundoImpl(); URLRegistro="rmi://localhost:"+numPuerto+"/holaMundo"; Naming.rebind(URLRegistro, objExportado); System.out.println("Servidor registrado. El registro contiene actualm

ente:"); listaRegistro(URLRegistro); System.out.println("Servidor preparado"); } catch (Exception excr){ System.out.println("Excepcin en Servidor.main: "+excr); } } private static void arrancarRegistro(int numPuertoRMI) throws RemoteException{ try{ Registry registro=LocateRegistry.getRegistry(numPuertoRMI); registro.list(); } catch(RemoteException e){ System.out.println("El registro RMI no se puede localizar en el puer to "+numPuertoRMI); Registry registro = LocateRegistry.createRegistry(numPuertoRMI); System.out.println("Registro RMI creado en el puerto: "+numPuertoRMI ); } } private static void listaRegistro(String URLRegistro) throws RemoteException, MalformedURLException{ System.out.println("Registro "+URLRegistro + " contiene: "); String[]nombres=Naming.list(URLRegistro); for(int i=0;i<nombres.length;i++) System.out.println(nombres[i]); } }

You might also like