Skip to main content
1 de 3
Ulises HR
  • 87
  • 1
  • 1
  • 5

2 clients in 1 Server With Threads

In distributed sistems class, we're making threads and sockets, the next project has the objective of having one server, which code is a little further, and have two clients that will connect to that client.

We're not able to make the process of the two clients at the same time, it only processs what's required from first client to connect, and when the process is done, the other client can't continue with its own.

What can we change, or add to our code, so it is possible to make the two connections and process separatel?

This is the server

public class Servidor {
public static void main(String args[])
{
    ServerSocket ss;
    
    try 
    {
        ss = new ServerSocket(4444);
        Socket s1 = ss.accept();
        conexiones con;
        con = new conexiones(s1);
        Thread hilo=new Thread(con);
        hilo.start();
    } 
    catch (IOException ex) 
    {
        Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE,null, ex);
    }
}}

class conexiones extends Thread{ 
Socket cliente;
InputStream is;
OutputStream os1;
DataInputStream dis;
DataOutputStream dos1;

conexiones(Socket c)
{
    this.cliente = c;
}

public void run()
{
    int x,y,factorial=1;
    int resultado = 0,resultado2[]=new int[10];
    String a,b,c, tipo;
   
   
    try 
    {
       is = cliente.getInputStream();
       os1 = cliente.getOutputStream();
       dis = new DataInputStream(is);
       dos1 = new DataOutputStream(os1);
        
        
        do
        {
            System.out.println("Esperando el tipo de operacion...");
            tipo = dis.readUTF();
            System.out.println("La operacion seleccionada es :" + tipo);

            if(tipo.equals("1"))
            {

                a = dis.readUTF();
                System.out.println("El numero para sacar el factorial es "+a);

                x=Integer.parseInt(a);
                for (int i=x;i>0;i--) 
                {
                    factorial=factorial*i;
                }

                resultado=factorial;

                System.out.println("El resultado es: " + resultado);
                dos1.writeUTF(Integer.toString(resultado));
            }
            else if(tipo.equals("2"))
            {
                a = dis.readUTF();
                System.out.println("Se seleccionaron "+a+" Para el fibonacci");

                x=Integer.parseInt(a);
                int secu=1;
                int secu2=1;
                int fibo[]=new int[10];


                fibo[1]=1;
                for (int i=2;i<=x;i++) 
                {
                    fibo[i]=secu2;

                    secu2=secu+secu2;
                    secu=secu2-secu;
                }

                resultado2=fibo;

                System.out.println("El resultado es: " +Arrays.toString(fibo));
                dos1.writeUTF(Arrays.toString(resultado2));
            }
            else if(tipo.equals("3"))
            {
                a = dis.readUTF();
                System.out.println("Se selecciono "+a+" como el numero minimo de la serie");

                b = dis.readUTF();
                System.out.println("Se selecciono "+b+" como el numero maximo de la serie");

                x=Integer.parseInt(a);
                y=Integer.parseInt(b);

                int serie[]=new int [10];
                int j=0;

                for(int i=x;i<=y;i++)
                {
                    serie[j]=i;
                    j++;
                }

                resultado2=serie;

                System.out.println("El resultado es: " +Arrays.toString(serie));
                dos1.writeUTF(Arrays.toString(resultado2));
            }
            else if(tipo.equals("4"))
            {
                System.out.println("Salio");
                dos1.writeUTF("Salio");
            }
        }while(!tipo.contains("4"));
        
    } 
    catch (IOException ex) 
    {
        Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

this is the client

public class Cliente2 extends Thread{
public static void main(String args[])
{
    try {
        String cad1,cad2,cad3;
        String resultado = "";
        Socket sc1 = new Socket("127.0.1.1",4444);
        InputStreamReader isr = new InputStreamReader( System.in );
        InputStream is = sc1.getInputStream();
        BufferedReader bf = new BufferedReader(isr);
        OutputStream os1 = sc1.getOutputStream();
        DataOutputStream dos1 = new DataOutputStream(os1);
        DataInputStream dis = new DataInputStream(is);
        
        do
        {
            System.out.println("Que tipo de operacion quieres");
            System.out.println("1)Factorial 2)Fibonacci 3)Serie 4)Salir");

            cad3 = bf.readLine();
            dos1.writeUTF(cad3);

            if(cad3.contains("1"))
            {
                System.out.println("Dame un numero para el factorial: ");
                cad1 = bf.readLine();
                dos1.writeUTF(cad1);

                resultado = dis.readUTF();
                System.out.println("El resultado es: " + resultado);
            }
            else if(cad3.contains("2"))
            {
                System.out.println("Cuantos dijitos de fibonacci quiere ver? ");
                cad1 = bf.readLine();
                dos1.writeUTF(cad1);

                resultado = dis.readUTF();
                System.out.println("El resultado es: " + resultado);
            }
            else if(cad3.contains("3"))
            {
                System.out.println("Ingrese el numero minimo de la serie");
                cad1 = bf.readLine();
                dos1.writeUTF(cad1);

                System.out.println("Ingrese el numero maximo de la serie");
                cad2 = bf.readLine();
                dos1.writeUTF(cad2);

                resultado = dis.readUTF();
                System.out.println("El resultado es: " + resultado);
            }
            else if(cad3.contains("4"))
            {
                resultado = dis.readUTF();
                System.out.println(resultado);
            }
            
        }while(!cad3.contains("4"));
        sc1.close();
    } catch (IOException ex) {
        Logger.getLogger(Cliente2.class.getName()).log(Level.SEVERE, null, ex);
    }
   
}    

}

Ulises HR
  • 87
  • 1
  • 1
  • 5