Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class ServidorHijo extends Thread
- {
- private Socket socket = null;
- PrintWriter out;
- int contador=0;
- int []monto=new int[4];
- public ServidorHijo(Socket socket, int contador)
- {
- super("ServidorHijo");
- System.out.println("Se Conecto Nuevo Cliente!!");
- System.out.println("Se Inicia Nueva Hebra de Servidor");
- this.socket = socket;
- this.contador=contador;
- this.monto[1]=1000;
- this.monto[2]=1000;
- this.monto[3]=1000;
- }
- public void run()
- {
- try
- {
- out = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
- String inputLine="hola", outputLine="";
- //inicio codigo nuevo
- /*
- String path="";
- System.out.println("Ingrese la dirección en donde desea recibir el archivo: " );
- path=stdIn.readLine();
- path+="/cancionnueva"+contador+".mp3";
- //path="C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3";
- InputStream llegada = socket.getInputStream();
- FileOutputStream destino=new FileOutputStream(path);
- byte[] buffer = new byte[117];
- int len;
- while((len=llegada.read(buffer))>0)
- {
- destino.write(buffer,0,len);
- //System.out.println("len vale:"+len);
- if(len!=117)
- break;
- }
- */
- //fin codigo nuevo
- //codigo abrir archivo inicio
- //FileInputStream fstream = new FileInputStream("C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3");
- /*
- FileInputStream fstream = new FileInputStream(path);
- FileInputStream fstream2 = new FileInputStream("C:/Users/Gustavo/Desktop/servidorfichero/cancion2.mp3");
- // Creamos el objeto de entrada
- DataInputStream entrada = new DataInputStream(fstream);
- DataInputStream entrada2 = new DataInputStream(fstream2);
- // Creamos el Buffer de Lectura
- BufferedReader bufferLectura = new BufferedReader(new InputStreamReader(entrada));
- BufferedReader bufferLectura2 = new BufferedReader(new InputStreamReader(entrada2));
- String strLinea,strLinea2;
- int sum=0,iguales=0;
- // Leer el archivo linea por linea
- while ((strLinea = bufferLectura.readLine()) != null && (strLinea2 = bufferLectura2.readLine()) != null)
- {
- // Imprimimos la línea por pantalla
- sum++;
- if(strLinea.equals(strLinea2))
- iguales++;
- //System.out.println (strLinea);
- }
- if(sum==iguales)
- {
- iguales=1;
- System.out.println("son iguales");
- }
- else
- {
- System.out.println("son iguales");
- iguales=0;
- }
- */
- //codigo abrir archivos fin
- System.out.println("listo");
- int flagg=0,rut;
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println(inputLine);
- if(flagg==0)//si el rut aùn no es valido
- {
- rut=Integer.parseInt(inputLine);
- System.out.println("el rut del cliente ("+contador+") es:"+rut);
- if((rut==17214311) || (rut==17539138) || (rut==18111635))
- {
- flagg=1;
- out.println("1");
- String path="";
- System.out.println("Ingrese la dirección en donde desea recibir el archivo: " );
- path=stdIn.readLine();
- path+="/cancionnueva"+contador+".mp3";
- //path="C:/Users/Alex Heisinger/Desktop/Servidor/johnwax"+contador+".mp3";
- InputStream llegada = socket.getInputStream();
- FileOutputStream destino=new FileOutputStream(path);
- byte[] buffer = new byte[117];
- int len;
- while((len=llegada.read(buffer))>0)
- {
- destino.write(buffer,0,len);
- //System.out.println("len vale:"+len);
- if(len!=117)
- break;
- }
- //inicio nuevo parte 2
- FileInputStream fstream = new FileInputStream(path);
- FileInputStream fstream2 = new FileInputStream("C:/Users/Gustavo/Desktop/servidorfichero/"+rut+"(cliente).mp3");
- // Creamos el objeto de entrada
- DataInputStream entrada = new DataInputStream(fstream);
- DataInputStream entrada2 = new DataInputStream(fstream2);
- // Creamos el Buffer de Lectura
- BufferedReader bufferLectura = new BufferedReader(new InputStreamReader(entrada));
- BufferedReader bufferLectura2 = new BufferedReader(new InputStreamReader(entrada2));
- String strLinea,strLinea2;
- int sum=0,iguales=0;
- // Leer el archivo linea por linea
- while ((strLinea = bufferLectura.readLine()) != null && (strLinea2 = bufferLectura2.readLine()) != null)
- {
- // Imprimimos la línea por pantalla
- sum++;
- if(strLinea.equals(strLinea2))
- iguales++;
- //System.out.println (strLinea);
- }
- if(sum==iguales)
- {
- iguales=1;
- System.out.println("son iguales");
- }
- else
- {
- System.out.println("son iguales");
- iguales=0;
- }
- //codigo abrir archivos fin
- }
- else
- {
- out.println("0");
- // out.println("El rut que usted a ingresado no pertenece a un cliente ingresado al sistema");
- //out.println("Intente con otro");
- }
- }
- else//si estamos en el caso de rut valido
- {
- if(Integer.parseInt(inputLine)==1)//en esta opcion retira dinero
- {
- //out.println("De cuanto es el monto?");
- inputLine = in.readLine();//lee monto del cliente
- if(monto[contador]-Integer.parseInt(inputLine)>=0)
- {
- out.println("Su retiro ha sido realizado de forma exitosa");
- monto[contador]-=Integer.parseInt(inputLine);
- }
- else
- {
- out.println("No es posible llevar a cabo su retiro, pues su monto quedaría en "+Integer.toString(monto[contador]-Integer.parseInt(inputLine)));
- }
- }
- else
- if(Integer.parseInt(inputLine)==2)//en esta opcion ve su monto
- {
- //out.println("De cuanto es el monto?");
- //inputLine = in.readLine();//lee respuesta cliente
- out.println("su saldo actual es "+Integer.toString(monto[contador]));
- }
- else
- if(Integer.parseInt(inputLine)==3)//salir
- out.println("Adios cliente "+Integer.toString(contador));
- }
- }
- out.close();
- in.close();
- socket.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement