Advertisement
Gustavo_Inzunza

ServidorHijo(coninterfaz)

Jun 3rd, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.98 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. public class ServidorHijo extends Thread
  9. {
  10.     private Socket socket = null;
  11.     PrintWriter out;
  12.     int contador=0;
  13.     int []monto=new int[4];
  14.     public ServidorHijo(Socket socket, int contador)
  15.     {
  16.         super("ServidorHijo");
  17.         System.out.println("Se Conecto Nuevo Cliente!!");
  18.         System.out.println("Se Inicia Nueva Hebra de Servidor");
  19.         this.socket = socket;
  20.         this.contador=contador;
  21.         this.monto[1]=1000;
  22.         this.monto[2]=1000;
  23.         this.monto[3]=1000;
  24.     }
  25.     public void run()
  26.     {  
  27.         try
  28.         {
  29.             out = new PrintWriter(socket.getOutputStream(), true);
  30.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  31.             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  32.             String inputLine="hola", outputLine="";
  33.             //inicio codigo nuevo
  34.             /*
  35.             String path="";
  36.             System.out.println("Ingrese la dirección en donde desea recibir el archivo: " );
  37.             path=stdIn.readLine();
  38.             path+="/cancionnueva"+contador+".mp3";
  39.             //path="C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3";
  40.             InputStream llegada = socket.getInputStream();
  41.             FileOutputStream destino=new FileOutputStream(path);
  42.             byte[] buffer = new byte[117];
  43.             int len;
  44.             while((len=llegada.read(buffer))>0)
  45.             {
  46.                 destino.write(buffer,0,len);
  47.                 //System.out.println("len vale:"+len);
  48.                 if(len!=117)
  49.                     break;
  50.             }
  51.              */
  52.             //fin codigo nuevo
  53.             //codigo abrir archivo inicio
  54.             //FileInputStream fstream = new FileInputStream("C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3");
  55.           /*
  56.             FileInputStream fstream = new FileInputStream(path);
  57.             FileInputStream fstream2 = new FileInputStream("C:/Users/Gustavo/Desktop/servidorfichero/cancion2.mp3");
  58.             // Creamos el objeto de entrada
  59.             DataInputStream entrada = new DataInputStream(fstream);
  60.             DataInputStream entrada2 = new DataInputStream(fstream2);
  61.             // Creamos el Buffer de Lectura
  62.             BufferedReader bufferLectura = new BufferedReader(new InputStreamReader(entrada));
  63.             BufferedReader bufferLectura2 = new BufferedReader(new InputStreamReader(entrada2));
  64.             String strLinea,strLinea2;
  65.             int sum=0,iguales=0;
  66.             // Leer el archivo linea por linea
  67.             while ((strLinea = bufferLectura.readLine()) != null && (strLinea2 = bufferLectura2.readLine()) != null)
  68.             {
  69.                 // Imprimimos la línea por pantalla
  70.                 sum++;
  71.                 if(strLinea.equals(strLinea2))
  72.                     iguales++;
  73.                 //System.out.println (strLinea);
  74.             }
  75.             if(sum==iguales)
  76.             {
  77.                 iguales=1;
  78.                 System.out.println("son iguales");
  79.             }
  80.             else
  81.             {
  82.               System.out.println("son iguales");
  83.                 iguales=0;
  84.             }
  85.            */
  86.             //codigo abrir archivos fin
  87.              System.out.println("listo");
  88.  
  89.             int flagg=0,rut;
  90.             while ((inputLine = in.readLine()) != null)
  91.             {
  92.                     System.out.println(inputLine);
  93.                      if(flagg==0)//si el rut aùn no es valido
  94.                      {
  95.                         rut=Integer.parseInt(inputLine);
  96.                         System.out.println("el rut del cliente ("+contador+") es:"+rut);
  97.                         if((rut==17214311) || (rut==17539138) || (rut==18111635))
  98.                         {
  99.                            flagg=1;
  100.                            out.println("1");
  101.                            String path="";
  102.                             System.out.println("Ingrese la dirección en donde desea recibir el archivo: " );
  103.                             path=stdIn.readLine();
  104.                             path+="/cancionnueva"+contador+".mp3";
  105.                             //path="C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3";
  106.                             InputStream llegada = socket.getInputStream();
  107.                             FileOutputStream destino=new FileOutputStream(path);
  108.                             byte[] buffer = new byte[117];
  109.                             int len;
  110.                             while((len=llegada.read(buffer))>0)
  111.                             {
  112.                                 destino.write(buffer,0,len);
  113.                                 //System.out.println("len vale:"+len);
  114.                                 if(len!=117)
  115.                                     break;
  116.                             }
  117.                            //inicio nuevo parte 2
  118.                             FileInputStream fstream = new FileInputStream(path);
  119.                             FileInputStream fstream2 = new FileInputStream("C:/Users/Gustavo/Desktop/servidorfichero/"+rut+"(cliente).mp3");
  120.                             // Creamos el objeto de entrada
  121.                             DataInputStream entrada = new DataInputStream(fstream);
  122.                             DataInputStream entrada2 = new DataInputStream(fstream2);
  123.                             // Creamos el Buffer de Lectura
  124.                             BufferedReader bufferLectura = new BufferedReader(new InputStreamReader(entrada));
  125.                             BufferedReader bufferLectura2 = new BufferedReader(new InputStreamReader(entrada2));
  126.                             String strLinea,strLinea2;
  127.                             int sum=0,iguales=0;
  128.                             // Leer el archivo linea por linea
  129.                             while ((strLinea = bufferLectura.readLine()) != null && (strLinea2 = bufferLectura2.readLine()) != null)
  130.                             {
  131.                                 // Imprimimos la línea por pantalla
  132.                                 sum++;
  133.                                 if(strLinea.equals(strLinea2))
  134.                                     iguales++;
  135.                                 //System.out.println (strLinea);
  136.                             }
  137.                             if(sum==iguales)
  138.                             {
  139.                                 iguales=1;
  140.                                 System.out.println("son iguales");
  141.                             }
  142.                             else
  143.                             {
  144.                               System.out.println("son iguales");
  145.                                 iguales=0;
  146.                             }
  147.                             //codigo abrir archivos fin
  148.                         }
  149.                         else
  150.                         {
  151.                             out.println("0");
  152.                            // out.println("El rut que usted a ingresado no pertenece a un cliente ingresado al sistema");
  153.                             //out.println("Intente con otro");
  154.                         }
  155.                     }
  156.                     else//si estamos en el caso de rut valido
  157.                     {
  158.                         if(Integer.parseInt(inputLine)==1)//en esta opcion retira dinero
  159.                         {
  160.                             //out.println("De cuanto es el monto?");
  161.                             inputLine = in.readLine();//lee monto del cliente
  162.                             if(monto[contador]-Integer.parseInt(inputLine)>=0)
  163.                             {
  164.                                 out.println("Su retiro ha sido realizado de forma exitosa");
  165.                                 monto[contador]-=Integer.parseInt(inputLine);
  166.                             }
  167.                             else
  168.                             {
  169.                                 out.println("No es posible llevar a cabo su retiro, pues su monto quedaría en "+Integer.toString(monto[contador]-Integer.parseInt(inputLine)));
  170.                             }
  171.                         }
  172.                         else
  173.                             if(Integer.parseInt(inputLine)==2)//en esta opcion ve su monto
  174.                             {
  175.                                 //out.println("De cuanto es el monto?");
  176.                                 //inputLine = in.readLine();//lee respuesta cliente
  177.                                 out.println("su saldo actual es "+Integer.toString(monto[contador]));
  178.                             }
  179.                             else
  180.                                 if(Integer.parseInt(inputLine)==3)//salir
  181.                                     out.println("Adios cliente "+Integer.toString(contador));
  182.                     }
  183.             }
  184.             out.close();
  185.             in.close();
  186.             socket.close();
  187.         }
  188.         catch (IOException e)
  189.         {
  190.             e.printStackTrace();
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement