Vous êtes sur la page 1sur 14

DAD

Prctica 12
Hundir la Flota empleando paneles grficos
Fco. Javier Vaquero y Rubn Snchez 17/01/2013

Nos adentramos en el desarrollo de aplicaciones cliente-servidor con Java-RMI empleando paneles grficos como interfaz con el usuario.

TAREA 1
HundirFlotaCliente.java:
package lab10_4; import java.rmi.RemoteException; import java.rmi.NotBoundException; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.util.*; public class HundirFlotaCliente { static public void main(String args[]){ HundirFlotaInterface objetoServidor; Registry registry; String serverAddress = new String("localHost"); String serverPort = new String("3232"); Scanner entrada = new Scanner(System.in); int fil,col; String respuesta; while(1!=2){ System.out.println("Introduzca el numero de fila (0-8): "); fil=entrada.nextInt(); System.out.println("Introduzca el numero de columna" " (0-9): "); col=entrada.nextInt(); System.out.println ("Enviando desde el cliente el disparo (" + fil + "," + col + ")"); System.out.println ("El envio se ha realizado desde el puerto " + serverPort + " de la direccin IP " + serverAddress); try{ registry=LocateRegistry.getRegistry (serverAddress,(new Integer(serverPort)).intValue()); objetoServidor=(HundirFlotaInterface) (registry.lookup("objetoServidor")); respuesta=objetoServidor.disparar(fil,col); System.out.println ("El disparo en (" + fil + "," + col + ") cae " "en " + respuesta); } // end try catch(RemoteException e){ e.printStackTrace(); } // end catch catch(NotBoundException e){ System.err.println(e); } // end catch } } // end main } // end class

HundirFlotaServidor.java:
package lab10_3; import java.net.InetAddress; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class HundirFlotaServidor extends java.rmi.server.UnicastRemoteObject implements HundirFlotaInterface{ String address; Registry registry; int[][] mapaBarcos = {{0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,0,0,0,0,0}, {0,1,0,1,0,1,0,1,1,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,1,0,0,1,1,1,1}, {0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,1,1}}; int[][] mapaDisparos = {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}; public void disparar(int x, int y) throws RemoteException{ String text1 = "BARCO"; String text2 = "AGUA"; System.out.println("El disparo recibido en el servidor es + " ("+ x + "," + y + ")"); if(mapaBarcos[x][y]==1){ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text1); return text1; } else{ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text2); return text2; } } // end receiveMessage() public HundirFlotaServidor() throws RemoteException{ try{ address = (InetAddress.getLocalHost()).toString(); } // end try catch(Exception e){ System.out.println("La clase InetAddress no pudo" + " obtener la direccin IP."); } int port=3232; System.out.println("El servidor est corriendo en la" + " direccin " + address + " y el puerto " + port); try{ registry = LocateRegistry.createRegistry(port); registry.rebind("objetoServidor", this); } // end try

catch(RemoteException e){ System.out.println("remote exception"+ e); } // end catch } // end RmiServer static public void main(String args[]){ try{ HundirFlotaServidor server = new HundirFlotaServidor(); } // end try catch (Exception e){ e.printStackTrace(); System.exit(1); } // end catch } // end main() } // end class

HundirFlotaInterface.java:
package lab10_2; import java.rmi.Remote; import java.rmi.RemoteException; public interface HundirFlotaInterface extends Remote{ String disparar(int x, int y) throws RemoteException;

TAREA 2
NameDialog.java: package lab11_2; import javax.swing.JOptionPane; public class NameDialog{ public static void main(String[] args){ String nombre = JOptionPane.showInputDialog("Cmo te llamas?"); //utilizamos la clase JOptionPane para crear una caja de //dilogo que ponga el texto incluido. //Adems al ser del tipo Input nos exigir escribir una //cadena de caracteres que se guardaran en el String //nombre String message = String.format("Feliz ao 2013, %s", nombre); //creamos una cadena de carcteres con lo incluido en el //parntesis JOptionPane.showMessageDialog(null, message); //mostramos en una caja de dilogo el String que hemos //creado anteriormente } }

TAREA 3
HundirFlotaCliente.java package lab11_3; import java.rmi.RemoteException; import java.rmi.NotBoundException; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.util.*; import javax.swing.JOptionPane;

public class HundirFlotaCliente { static public void main(String args[]){ HundirFlotaInterface objetoServidor; Registry registry; String serverAddress = new String("localHost"); String serverPort = new String("3232"); Scanner entrada = new Scanner(System.in); int fil,col; String respuesta; while(1!=2){ fil = Integer.parseInt(JOptionPane.showInputDialog("Introduzca e l" "numero de fila (0-8)")); col = Integer.parseInt(JOptionPane.showInputDialog("Introduzca el " "numero de columna (0-9)")); System.out.println ("Enviando desde el cliente el disparo (" + fil + "," + col + ")"); System.out.println ("El envio se ha realizado desde el puerto " + serverPort +" de" " la direccin IP " + serverAddress);

try{ registry=LocateRegistry.getRegistry (serverAddress,(new Integer(serverPort)).intValue()); objetoServidor=(HundirFlotaInterface) (registry.lookup("objetoServidor"));

respuesta=objetoServidor.disparar(fil,col); String message = String.format ("El disparo en (" + fil + "," + col + ") cae en " + respuesta); JOptionPane.showMessageDialog(null, message); } // end try catch(RemoteException e){ e.printStackTrace(); } // end catch

catch(NotBoundException e){ System.err.println(e); } // end catch } } // end main } // end class

HundirFlotaInterface.java package lab11_3; import java.rmi.Remote; import java.rmi.RemoteException; public interface HundirFlotaInterface extends Remote{ String disparar(int x, int y) throws RemoteException; }

HundirFlotaServidor.java package lab11_3; import java.net.InetAddress; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.swing.JOptionPane; public class HundirFlotaServidor extends java.rmi.server.UnicastRemoteObject implements HundirFlotaInterface{ String address; Registry registry; int[][] mapaBarcos = {{0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,0,0,0,0,0}, {0,1,0,1,0,1,0,1,1,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,1,0,0,1,1,1,1}, {0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,1,1}}; int[][] mapaDisparos = {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}; public String disparar(int x, int y) throws RemoteException{ String text1 = "BARCO"; String text2 = "AGUA"; System.out.println("El disparo recibido en el servidor es ("+ x + "," + y + ")"); if(mapaBarcos[x][y]==1){ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text1); return text1; } else{ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text2); return text2; } } // end receiveMessage()

public HundirFlotaServidor() throws RemoteException{ try{ address = (InetAddress.getLocalHost()).toString(); } // end try catch(Exception e){ System.out.println("La clase InetAddress no pudo obtener la direccin IP."); }

int port=3232; System.out.println("El servidor est corriendo en la direccin " + address + " y el puerto " + port); try{ registry = LocateRegistry.createRegistry(port); registry.rebind("objetoServidor", this); } // end try

catch(RemoteException e){ System.out.println("remote exception"+ e); } // end catch } // end RmiServer static public void main(String args[]){ try{ HundirFlotaServidor server = new HundirFlotaServidor(); } // end try catch (Exception e){ e.printStackTrace(); System.exit(1); } // end catch } // end main() } // end class

Tarea 4
HundirFlotaCliente.java package lab11_4; import java.rmi.RemoteException; import java.rmi.NotBoundException; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.util.*; import javax.swing.*;

public class HundirFlotaCliente { static public void main(String args[]){ HundirFlotaInterface objetoServidor; Registry registry; String serverAddress = new String("localHost"); String serverPort = new String("3232"); int fil,col; String disparos; String respuesta; String message1; String message2; //StringBuffer matriz = new StringBuffer(); String[] matriz; while(1!=2){ fil = Integer.parseInt(JOptionPane.showInputDialog("Introduzca el" " numero de fila (0-8)")); col = Integer.parseInt(JOptionPane.showInputDialog("Introduzca el " "numero de columna (0-9)")); System.out.println ("Enviando desde el cliente el disparo (" + fil + "," + col + ")"); System.out.println ("El envio se ha realizado desde el puerto " + serverPort + " de " la direccin IP " + serverAddress);

try{ registry=LocateRegistry.getRegistry (serverAddress,(new Integer(serverPort)).intValue()); objetoServidor=(HundirFlotaInterface) (registry.lookup("objetoServidor")); // call the remote method respuesta=objetoServidor.disparar(fil,col); disparos = objetoServidor.recibirMatriz(); message1 = String.format ("El disparo en (" + fil + "," + col + ") cae en " + respuesta); JOptionPane.showMessageDialog(null, message1); message2 = String.format(disparos);

JOptionPane.showMessageDialog(null, message2); } // end try catch(RemoteException e){ e.printStackTrace(); } // end catch catch(NotBoundException e){ System.err.println(e); } // end catch } } // end main } // end class

HundirFlotaInterface.java package lab11_4; import java.rmi.Remote; import java.rmi.RemoteException; public interface HundirFlotaInterface extends Remote{ String disparar(int x, int y) throws RemoteException; String recibirMatriz() throws RemoteException; }

HundirFlotaServidor.java

package lab11_4; import java.net.InetAddress; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import javax.swing.JOptionPane; public class HundirFlotaServidor extends java.rmi.server.UnicastRemoteObject implements HundirFlotaInterface{ String address; Registry registry; int[][] mapaBarcos = {{0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,0,0,0,0,0}, {0,1,0,1,0,1,0,1,1,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0}, {0,1,0,1,0,0,1,1,1,1}, {0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,1,1}}; int[][] mapaDisparos = {{0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}; public String disparar(int x, int y) throws RemoteException{ String text1 = "BARCO"; String text2 = "AGUA"; System.out.println("El disparo recibido en el servidor es ("+ x + "," + y + ")"); if(mapaBarcos[x][y]==1){ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text1); mapaDisparos[x][y]=2; return text1; } else{ System.out.println("El disparo en ("+ x + "," + y + ") cae en " + text2); mapaDisparos[x][y]=1; return text2; } } // end receiveMessage() public String recibirMatriz() throws RemoteException{ String matriz1=""; for(int i=0; i<mapaDisparos.length; i++){ for(int j=0; j<mapaDisparos.length; j++){ matriz1 += String.valueOf(mapaDisparos[i][j]); matriz1 += " "; }

matriz1 += "\n"; } return matriz1; } public HundirFlotaServidor() throws RemoteException{ try{ address = (InetAddress.getLocalHost()).toString(); } // end try catch(Exception e){ System.out.println("La clase InetAddress no pudo obtener la direccin IP."); } int port=3232; System.out.println("El servidor est corriendo en la direccin " + address + " y el puerto " + port); try{ registry = LocateRegistry.createRegistry(port); registry.rebind("objetoServidor", this); } // end try

catch(RemoteException e){ System.out.println("remote exception"+ e); } // end catch } // end RmiServer static public void main(String args[]){ try{ HundirFlotaServidor server = new HundirFlotaServidor(); } // end try catch (Exception e){ e.printStackTrace(); System.exit(1); } // end catch } // end main() } // end class

Vous aimerez peut-être aussi