Вы находитесь на странице: 1из 6

UNIVERSIDAD REGIONAL AUTNOMA DE LOS ANDES

UNIANDES IBARRA

FACULTAD DE SISTEMAS MERCANTILES

CARRERA DE SISTEMAS

DESARROLLO CLIENTE SERVIDOR II

TEMA: RMI ejemplos

ALEXIS MRQUEZ

OCTAVO SISTEMAS
ING. RITA DAZ MSC.
16/05/2016
ABRIL - SEPTIEMBRE 2016

Tema: RMI(JAVA).
Objetivo: Realizar ejercicios utilizando tecnologa java con interfaces
RMI.
4.- Power Service
El objetivo es implementar un servicio con 2 mtodos. Uno llamado square
que calcula el cuadrado de un nmero entero dado y otro llamado power
que eleva un nmero entero a cualquier potencia entera y regresa el
resultado al cliente. Sugerencia, para elevar un nmero a una determinada
potencia utilice la clase BigInteger o la clase Math.
Interfaz
package cuatro;
/**
*
* @author ALXOZ
*/
public interface interfaz extends java.rmi.Remote {
public int cuadradro(int a) throws java.rmi.RemoteException;
public int elevado(int a,int b) throws java.rmi.RemoteException;
}
Cliente
package cuatro;
import
import
import
import
import

java.rmi.NotBoundException;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;
java.util.Scanner;

/**
*
* @author ALXOZ
*/
public class cliente {
public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
interfaz testRemote = (interfaz) registry.lookup("Operaciones");
Scanner leer = new Scanner(System.in);
System.out.println("Eliga el tipo de Operacion \n 1 elevar al cuadrado \n 2 elevar un
numero a una potencia ");
int valor = leer.nextInt();
switch (valor) {
case 1:
System.out.println("Numero 1");
int uno = leer.nextInt();
System.out.println("numero al cuadrado es : " + testRemote.cuadradro(uno));
break;
case 2:
System.out.println("Numero 1");
int unos = leer.nextInt();

System.out.println("Numero 2");
int dos = leer.nextInt();
System.out.println("el numero elevado es : " + testRemote.elevado(unos, dos));
break;
}
}
}
Servidor
package cuatro;
import
import
import
import
import
import

java.rmi.AlreadyBoundException;
java.rmi.Remote;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;
java.rmi.server.UnicastRemoteObject;

/**
*
* @author ALXOZ
*/
public class servidor {
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
Remote stub = UnicastRemoteObject.exportObject(new interfaz() {
@Override
public int cuadradro(int a) throws RemoteException {
double valor = Math.pow(a, 2);
return (int) valor;
}
@Override
public int elevado(int a, int b) throws RemoteException {
double valor = Math.pow(a, b);
return (int) valor;
}
}, 0);
Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
registry.bind("Operaciones", stub);
}
}

6.- LightBulb

Escriba un programa servidor para el control de un foco. Los clientes deben


poder encender y apagar el foco y preguntar su estado actual.
Interfaz
package cinco;
/**
*
* @author ALXOZ
*/
public interface interfaz extends java.rmi.Remote{
public void iestado(int a) throws java.rmi.RemoteException;

public String vestado() throws java.rmi.RemoteException;


}
Cliente
package cinco;
import
import
import
import
import

java.rmi.NotBoundException;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;
java.util.Scanner;

/**
*
* @author ALXOZ
*/
public class cliente {
public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
interfaz testRemote = (interfaz) registry.lookup("Estado");
Scanner leer = new Scanner(System.in);
int valor = 0;
while (valor != 3) {
System.out.println("Eliga el tipo de Operacion \n 1 ingresar estado \n 2 ver
estado \n 3 salir ");
System.out.println("");
valor = leer.nextInt();
switch (valor) {
case 1:
System.out.println("Ingrese 1 encender 0 apagar");
System.out.println("");
int uno = leer.nextInt();
testRemote.iestado(uno);
break;
case 2:
System.out.println("El estado actual es : " + testRemote.vestado());
System.out.println("");
break;
}
}
}
}
Servidor
package cinco;
import
import
import
import
import
import

java.rmi.AlreadyBoundException;
java.rmi.Remote;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;
java.rmi.server.UnicastRemoteObject;

/**
*
* @author ALXOZ
*/

public class servidor {


public static String estado = "";
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
Remote stub = UnicastRemoteObject.exportObject(new interfaz() {
@Override
public void iestado(int a) throws RemoteException {
if (a == 1) {
estado = "encendido";
} else {
estado = "apagado";
}
}
@Override
public String vestado() throws RemoteException {
return estado;
}
}, 0);
Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
registry.bind("Estado", stub);
}
}

8.- Time Stamp


Construya un servidor RMI que regrese al cliente la fecha y la hora de la
invocacin.
Interfaz
*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package seis;
/**
*
* @author ALXOZ
*/
public interface interfaz extends java.rmi.Remote {
public String hora() throws java.rmi.RemoteException;
}
Cliente
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package seis;
import
import
import
import
/**

java.rmi.NotBoundException;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;

*
* @author ALXOZ
*/
public class cliente {
public static void main(String[] args) throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry();
interfaz testRemote = (interfaz) registry.lookup("hora");
System.out.println("Hora de invocacion : " + testRemote.hora());
System.out.println("");
}
}
Servidor
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package seis;
import
import
import
import
import
import
import

java.rmi.AlreadyBoundException;
java.rmi.Remote;
java.rmi.RemoteException;
java.rmi.registry.LocateRegistry;
java.rmi.registry.Registry;
java.rmi.server.UnicastRemoteObject;
java.util.Date;

/**
*
* @author ALXOZ
*/
public class servidor {
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
Remote stub = UnicastRemoteObject.exportObject(new interfaz() {
@Override
public String hora() throws RemoteException {
Date hora = new Date();
return "" + hora;
}
}, 0);
Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
registry.bind("hora", stub);
}
}

Вам также может понравиться