Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| dad:ejercicioprotocolos [2023/11/09 19:52] – thejuanvisu | dad:ejercicioprotocolos [2023/11/23 11:30] (actual) – thejuanvisu | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Ejemplo de como podría ser el examen de Dad ====== | ====== Ejemplo de como podría ser el examen de Dad ====== | ||
| + | Probablemente se nos pida algo del estilo a la implementación de un protocolo, en el que un servidor deberá recibir comandos de un cliente y responder en función a estos. | ||
| - | Primero implementamos | + | ===== Elemento ===== |
| - | < | + | Este será la clase con el que trabajaremos: |
| + | < | ||
| + | public class Elemento{ | ||
| + | // | ||
| + | String nombre | ||
| + | double cantidad; | ||
| + | double veces = 0; | ||
| + | |||
| + | // | ||
| + | public Elemento(String nombre, double cantidad){ | ||
| + | this.nombre = nombre; | ||
| + | this.cantidad = cantidad; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Cliente ===== | ||
| + | |||
| + | Este será el cliente que utilizaremos para enviar señales al serverSocket, | ||
| + | <code java Cliente.java> | ||
| public class Cliente{ | public class Cliente{ | ||
| - | try{//Se debe hacer siempre try Catch cuando andamos con sockets | + | |
| - | socket = new Socket(" | + | |
| + | socket = new Socket(" | ||
| | | ||
| - | | + | |
| - | BufferedReader br = new BufferedReader(new InputStramReader(socket.getInputStream())); | + | BufferedReader br = new BufferedReader(new InputStramReader(socket.getInputStream())); |
| - | PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); | + | PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); |
| | | ||
| - | | + | |
| - | String rl = ""; | + | String rl = ""; |
| | | ||
| - | | + | |
| - | pw.println(sc.nextLine());// | + | pw.println(sc.nextLine());// |
| - | pw.flush(); Limpiamos salida | + | pw.flush(); Limpiamos salida |
| | | ||
| - | | + | |
| - | System.out.println((rl = br.readLine())) | + | System.out.println((rl = br.readLine())) |
| - | }while(lineaLeida.contentEquals(" | + | }while(lineaLeida.contentEquals(" |
| | | ||
| - | | + | |
| - | e.printStackTrace(); | + | e.printStackTrace(); |
| + | } | ||
| } | } | ||
| } | } | ||
| - | |||
| public static void main(String[] args){ | public static void main(String[] args){ | ||
| (new Cliente()).ejecutar(); | (new Cliente()).ejecutar(); | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | ===== Servidor ===== | ||
| + | <code java Server.java> | ||
| + | public class Server{ | ||
| + | ServerSocket serverSocket;// | ||
| + | Socket socket;// | ||
| + | | ||
| + | Static Hashtable< | ||
| + | | ||
| + | public void ejecutar(){ | ||
| + | try{ | ||
| + | serverSocket = new ServerSocket(5000) //Indicamos puerto de escucha | ||
| + | while(true){ | ||
| + | socket=serverSocket.accept();// | ||
| + | (new ServerThread(socket)).start()// | ||
| + | } | ||
| + | }catch(IOException e){ | ||
| + | e.printStackTrace()// | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static void main(String[] args){ | ||
| + | (new Server()).ejecutar(); | ||
| + | } | ||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== ServerThread ===== | ||
| + | En el ServerThread extendemos la funcionalidad de Thread e implementamos la funcionalidad del server Socket. En este caso lo que hará nuestro servidor es reaccionar a los comandos que se le manden y en caso de recibir un comando inválido enviar un mensaje indicando que el comando no es válido. | ||
| + | |||
| + | <code java ServerThread.java> | ||
| + | public class ServerThread extends Thread{ | ||
| + | | ||
| + | Socket socket; Declaramos Socket | ||
| + | | ||
| + | public ServerThread(Socket socket){// | ||
| + | this.socket = socket | ||
| + | } | ||
| + | | ||
| + | public void run(){// | ||
| + | try{ | ||
| + | System.out.println(" | ||
| + | BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||
| + | PrintWriter pr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); | ||
| + | string[] linealeida; | ||
| + | | ||
| + | do{ | ||
| + | linealeida = br.readLine().split("" | ||
| + | | ||
| + | switch(lineaLeida[0]){ | ||
| + | //Comando ADD, formato: ADD nombre cantidad (add patata 1) | ||
| + | case " | ||
| + | if(lineaLeida.length==3){Si el comando tiene 3 elementos (Comando, nombre, cantidad) | ||
| + | ArrayList< | ||
| + | Elemento elemento = new Elemento(lineaLeida[1], | ||
| + | elementos= Server.listaElementos.get(linealeida[1]); | ||
| + | | ||
| + | if(elementos==null){// | ||
| + | elementos = new ArrayList< | ||
| + | Server.listaElementos.put(elemento.nombre, | ||
| + | } | ||
| + | | ||
| + | Elementos.add(elemento); | ||
| + | pw.println(" | ||
| + | pw.flush; | ||
| + | }else{ | ||
| + | pw.println(linealeida[0] + "El comando necesita mas argumentos\nEstructura: | ||
| + | pw.flush(); | ||
| + | } | ||
| + | break; | ||
| + | | ||
| + | //Comando GET, formato: Get Nombre (Get patata) | ||
| + | case " | ||
| + | if(linealeida.legth==2){ //Si el comando tiene GET + el Nombre | ||
| + | ArrayList< | ||
| + | elementos = Server.listaElementos.get(lineaLeida[1]);// | ||
| + | | ||
| + | if(elementos == null){//Si no se encuentra el elemento | ||
| + | pw.println(" | ||
| + | pw.flush; | ||
| + | }else{ | ||
| + | double total = 0; | ||
| + | for(Elemento elemento: elementos){ | ||
| + | total+=elemento.cantidad; | ||
| + | } | ||
| + | pw.println(total); | ||
| + | pw.flush; | ||
| + | } | ||
| + | | ||
| + | }else{ | ||
| + | pw.println(linealeida[0]+" | ||
| + | pw.flush(); | ||
| + | } | ||
| + | break; | ||
| + | | ||
| + | //Comando DELETE, uso: DELETE Nombre (DELETE Patata) | ||
| + | case " | ||
| + | if(linealeida.lenght == 2){ | ||
| + | ArrayList< | ||
| + | Elementos = Server.listaElementos.get(lineaLeida[1]); | ||
| + | if((Elementos==null || Elementso.size()==0)){ | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | |||
| + | }else{ | ||
| + | Elementos.remove(elementos.size()-1); | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | } | ||
| + | }else{ | ||
| + | pw.println(linealeida[0] + "se ha usado incorrectamente\nUso: | ||
| + | } | ||
| + | break; | ||
| + | | ||
| + | | ||
| + | | ||
| + | //Comando de salida EXIT | ||
| + | case " | ||
| + | if(lineaLeida.leght==1){ | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | }else{ | ||
| + | pw.printl(" | ||
| + | pw.flush(); | ||
| + | } | ||
| + | break; | ||
| + | | ||
| + | //En caso de introducir comando invalido | ||
| + | default: | ||
| + | pw.prinln(" | ||
| + | pw.flush; | ||
| + | break; | ||
| + | | ||
| + | | ||
| + | } | ||
| + | }while(linealleida[0].conentEquals(" | ||
| + | | ||
| + | }catch(IOException e){ | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== DELETE ===== | ||
| + | |||
| + | <code java> | ||
| + | case " | ||
| + | |||
| + | if(linealeida.length == 2) { | ||
| + | ArrayList< | ||
| + | |||
| + | elementos = Server.listaElementos.get(linealeida[1]); | ||
| + | |||
| + | if(elementos == null) { | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | }else { | ||
| + | elementos.remove(elementos.size()-1); | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | } | ||
| + | |||
| + | }else { | ||
| + | pw.println(" | ||
| + | pw.flush(); | ||
| + | } | ||
| + | |||
| + | break; | ||
| </ | </ | ||