Advertisement
alduncin

Servidor.java

Nov 29th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 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.net.*;
  12. import java.io.*;
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.FocusEvent;
  17. import java.io.ObjectInputStream;
  18. import java.io.ObjectOutputStream;
  19. import java.io.DataInputStream;
  20. import java.net.Socket;
  21.  
  22.  
  23. public class Servidor {
  24.   public static void main (String [] args ) throws Exception {
  25.     int c = 1;//Inicializamos con un cliente
  26.     double suma = 0;
  27.     ObjectOutputStream oos = null;
  28.     ObjectInputStream  ois = null;
  29.     DataInputStream  dis = null;
  30.     ServerSocket servidor = new ServerSocket(800);
  31.     ServerSocket serv = new ServerSocket(900);
  32.    
  33.     /**
  34.     NUMERO DE CLIENTES QUE EL SERVIDOR PODRA ACEPTAR EN ESTE CASO 2.
  35.     **/    
  36.     while (c<=2) {
  37.       System.out.println("Esperando...");//informativo
  38.  
  39.       Socket socket = servidor.accept();
  40.       System.out.println("Conexion Aceptada Desde Direccion IP : " + socket.getInetAddress());
  41.       if(c==1){
  42.         /**
  43.         EDITAR LA DIRECCION DEL ARCHIVO DE TEXTO DE ENTRADA
  44.         **/
  45.           File archivo = new File ("C:/GananciasSuc1.txt");
  46.        
  47.           byte [] arreglo  = new byte [(int)archivo.length()];
  48.           FileInputStream entrada = new FileInputStream(archivo);
  49.           BufferedInputStream buf = new BufferedInputStream(entrada);
  50.           buf.read(arreglo,0,arreglo.length);
  51.           OutputStream envio = socket.getOutputStream();
  52.           System.out.println("Enviado...");//informativo
  53.           envio.write(arreglo,0,arreglo.length);
  54.           envio.flush();
  55.           socket.close();
  56.       }
  57.       else if(c==2){
  58.             /**
  59.         EDITAR LA DIRECCION DEL ARCHIVO DE TEXTO DE ENTRADA
  60.         **/
  61.           File archivo = new File ("C:/GananciasSuc2.txt");
  62.          
  63.       byte [] arreglo  = new byte [(int)archivo.length()];
  64.           FileInputStream entrada = new FileInputStream(archivo);
  65.       BufferedInputStream buf = new BufferedInputStream(entrada);
  66.           buf.read(arreglo,0,arreglo.length);
  67.           OutputStream envio = socket.getOutputStream();
  68.           System.out.println("Enviado...");
  69.           envio.write(arreglo,0,arreglo.length);
  70.           envio.flush();
  71.          
  72.           socket.close();  
  73.       }
  74.      
  75.       Socket socket1 = serv.accept();
  76.      
  77.       dis = new DataInputStream(socket1.getInputStream());
  78.       double n = (double)dis.readDouble();
  79.       suma = suma+n;
  80.  
  81.       /**  
  82.           EDITAR LA CANTIDAD DE CLIENTES DE SER NECESARIO
  83.        **/      
  84.       if(c==2){
  85.         System.out.println("Cantidad Total Recibida:"+suma);
  86.       }
  87.       c++;
  88.     }
  89.   }
  90. }
  91.  
  92.  
  93. /**
  94. **Si es necesario utilizar este programa al implementarlo con
  95. **mayor cantidad de clientes solo se tendra que agregar el sig.
  96. **codigo dentro del while(c<=deClientes)teniendo en cuenta una
  97. **salida de texto para cada uno:
  98.  
  99.   else if(c==3)
  100.        {
  101.       File archivo = new File ("C:/ArchivoSucursal#.txt");//:3
  102.           byte [] arreglo  = new byte [(int)archivo.length()];
  103.           FileInputStream entrada = new FileInputStream(archivo);
  104.           BufferedInputStream buf = new BufferedInputStream(entrada);
  105.           buf.read(arreglo,0,arreglo.length);
  106.           OutputStream envio = socket.getOutputStream();
  107.           System.out.println("Enviado...");
  108.           envio.write(arreglo,0,arreglo.length);
  109.           envio.flush();
  110.          
  111.           socket.close();  
  112.       }else if(c==4)
  113.       {
  114.           File archivo = new File ("C:/ArchivoSucursal#.txt");//:D
  115.           byte [] arreglo  = new byte [(int)archivo.length()];
  116.           FileInputStream entrada = new FileInputStream(archivo);
  117.           BufferedInputStream buf = new BufferedInputStream(entrada);
  118.           buf.read(arreglo,0,arreglo.length);
  119.           OutputStream envio = socket.getOutputStream();
  120.           System.out.println("Enviado...");
  121.           envio.write(arreglo,0,arreglo.length);
  122.           envio.flush();
  123.          
  124.           socket.close();  
  125.       }
  126. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement