Advertisement
alduncin

Cliente.java

Nov 29th, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /**
  2. **Proyecto: Algoritmo distribuido
  3. **Materia: Laboratorio de Sistemas Distribuidos y Paralelos
  4. **Jueves V1
  5. **Equipo: 3
  6. ** Gabriela Martinez Aldape
  7. ** Karen Alduncin Ibarra   
  8. ** Mario Morales Nieto
  9. **/
  10.  
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.io.DataOutputStream;
  14. import java.net.*;
  15. import java.awt.event.ActionEvent;
  16. import java.io.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19. import java.awt.event.FocusEvent;
  20.  
  21. import java.net.Socket;
  22.  
  23. public class Cliente{
  24.   public static void main (String [] args ) throws Exception {
  25.     File archivo1 = null;//se definen variables para leer un archivo
  26.     FileReader fr1 = null;//definimos lector
  27.     BufferedReader br1 = null;//definimos buffer para lectura
  28.     int t=0;
  29.     double suma=0,sumatotal=0;
  30.     ObjectOutputStream oos = null;
  31.     ObjectInputStream  ois = null;
  32.     DataOutputStream dos = null;
  33.     int  tamaño=999999999;
  34.     int leo;
  35.     int contados = 0;
  36.  
  37.     /**
  38.     PARTE DEL CODIGO EN DONDE SE MODIFICARAN LAS DIRECCIONES IP DE LOS INVOLUCRADOS PARA REALIZAR LA CONEXION CORRECTAMENTE
  39.     **/    
  40.  
  41.     Socket socket = new Socket("169.254.123.79",800);
  42.  
  43.     System.out.println("Conectando...");//informativo :P
  44.     System.out.println("...");
  45.     System.out.println("Conectado...");
  46.  
  47.     byte [] arreglo  = new byte [tamaño];
  48.     InputStream entra = socket.getInputStream();
  49.  
  50.     /**
  51.     EDITAR LA DIRECCION DEL ARCHIVO DE TEXTO EL CUAL SERA NUESTRA SALIDA DEL PROGRAMA
  52.     **/    
  53.  
  54.     FileOutputStream salida = new FileOutputStream("C:/Users/GABY/Desktop/GananciasGas.txt");
  55.  
  56.     BufferedOutputStream buf = new BufferedOutputStream(salida);
  57.     leo = entra.read(arreglo,0,arreglo.length);
  58.     contados = leo;
  59.  
  60.     //realiza la lectura de los datos
  61.     do {
  62.        leo =entra.read(arreglo, contados, (arreglo.length-contados));
  63.        if(leo >= 0) contados += leo;
  64.     } while(leo > -1);
  65.  
  66.     buf.write(arreglo, 0 , contados);//lo escribe en el buffer
  67.     buf.flush();//limpia la memoria del buffer
  68.  
  69.     /**
  70.     EDITAR LA DIRECCION DEL ARCHIVO DE TEXTO AL CUAL LE DAREMOS ENTRADA
  71.     **/    
  72.  
  73.     archivo1 = new File ("C:/Users/GABY/Desktop/GananciasGas.txt");
  74.  
  75.     fr1 = new FileReader (archivo1);
  76.     br1 = new BufferedReader(fr1);
  77.  
  78.     /**
  79.     Aqui se realiza la suma de las ganancias de la empresa en cuestion
  80.     **/
  81.     String linea1;
  82.     while((linea1=br1.readLine())!=null)
  83.     {
  84.        //System.out.println(linea1);
  85.        suma = Double.parseDouble(linea1);
  86.        sumatotal = sumatotal + suma;
  87.     t++;
  88.     }
  89.    
  90.     System.out.println("Suma Total: "+sumatotal);
  91.     //System.out.println(t);
  92.     double sp = sumatotal;
  93.     dos = new DataOutputStream(socket.getOutputStream());
  94.     dos.writeDouble(sp);
  95.  
  96.     /**
  97.     Se cierra buffer, sockets y toda la comunicacion con el servidor.
  98.     **/    
  99.     buf.close();
  100.     socket.close();
  101.  
  102.     /**
  103.     EDITAR LA DIRECCION IP del socket
  104.     **/    
  105.     Socket socket1 = new Socket("169.254.123.79",900);
  106.     dos = new DataOutputStream(socket1.getOutputStream());
  107.     dos.writeDouble(sp);
  108.     socket1.close();
  109.    
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement