Error in client / server java

0

I'm starting with the subject of sockets in java and I have an exercise in which they ask me to read a file that asks for it with the client and the server shows me its connectivity.

public class Servidor extends Thread {

static final int port = 49000;
Socket skCliente;

public Servidor(Socket sCliente) {
    skCliente = sCliente;
}

public static void main(String[]args){
    try {

      ServerSocket skServidor = new ServerSocket(port);
      System.out.println("Servidor escuchando en el puerto " + port);

      while(true){
        Socket skCliente = skServidor.accept();
        System.out.println("¡¡¡Cliente Conectado!!!");
        new Servidor(skCliente).start();

      }



    } catch (Exception e) {
        System.out.println("Error de conexión");
    }
}

@Override
public void run() {
    try {  
    OutputStream aux = skCliente.getOutputStream();
    DataOutputStream flujo_salida = new DataOutputStream(aux);

    InputStream auxi = skCliente.getInputStream();
    DataInputStream flujo_entrada = new DataInputStream(auxi);

    flujo_salida.writeUTF("¡¡¡Conexión satisfactoria!!!");//Escribe en el Stream para que lo lea el cliente
    String comando;
    boolean correcto = false;

        do {    

                    flujo_salida.writeUTF("Introduce nombre del archivo: ");
                    comando=flujo_entrada.readUTF();
                    if(comando.equals("dam")){
                        File archivo = new File ("C:\Users\Dani\Desktop\dam.txt");
                        FileReader fr = new FileReader (archivo);
                        BufferedReader br = new BufferedReader(fr);
                        String linea = br.readLine();
                        flujo_salida.writeUTF("true");
                        flujo_salida.writeUTF(linea);
                        correcto = true;





                    }else{
                        flujo_salida.writeUTF("false");
                        correcto = false;




            }

        } while (correcto == false);
    flujo_entrada.close();
    flujo_salida.close();

    System.out.println("Cliente desconectado");
    skCliente.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

And this the client:

public class Cliente {


public static void main(String[] args) throws IOException {

    final String ip = "localhost";
    final int port = 49000;
    String resultado = "false";

    Scanner sc = new Scanner(System.in);
    Socket sCliente = new Socket(ip, port);
    //System.out.println("Me he conectado al servidor");

    InputStream aux = sCliente.getInputStream();
    DataInputStream flujo_entrada = new DataInputStream(aux);

    OutputStream auxi = sCliente.getOutputStream();
    DataOutputStream flujo_salida = new DataOutputStream(auxi);

    System.out.println(flujo_entrada.readUTF());
    System.out.print(flujo_entrada.readUTF());

    String archivo = sc.nextLine();
    flujo_salida.writeUTF(archivo);
    resultado = flujo_entrada.readUTF();

    do{
        if(resultado.equals("false")){


       System.out.println("No existe el archivo");
            System.out.print("Introduzca el nombre del archivo: ");
            archivo = sc.nextLine();
            flujo_salida.writeUTF(archivo); }


        if(resultado.equals("true")){
            System.out.println("Lectura del archivo solicitado: ");
            System.out.println(flujo_entrada.readUTF());
            System.out.println(flujo_entrada.readUTF());
        }
    }while (resultado.equals("false")); 
    sCliente.close();
}

}

The problem is that when I enter dam it shows me the content correctly and when I enter the wrong word it asks me to enter again but when I enter dam again it tells me that it is incorrect and that I re-enter the word.

    
asked by Daniel Cabrera Peraza 18.02.2018 в 23:15
source

1 answer

0

At the end of your iteration, when the input command is incorrect, your server is typing "Enter the name of the file:" as soon as the client receives the result, therefore always receives something other than String false or true .

The code for both should look like this:

For the server

import java.io.*;
import java.net.*;

public class Servidor extends Thread {

    static final int port = 49000;
    Socket skCliente;

    public Servidor(Socket sCliente) {
        skCliente = sCliente;
    }

    public static void main(String[]args){
        try {
            ServerSocket skServidor = new ServerSocket(port);
            System.out.println("Servidor escuchando en el puerto " + port);

            while(true){
                Socket skCliente = skServidor.accept();
                System.out.println("¡¡¡Cliente Conectado!!!");
                new Servidor(skCliente).start();
            }
        } catch (Exception e) {
            System.out.println("Error de conexión");
        }
    }

    @Override
    public void run() {
        try {  
             OutputStream aux = skCliente.getOutputStream();
             DataOutputStream flujo_salida = new DataOutputStream(aux);

             InputStream auxi = skCliente.getInputStream();
             DataInputStream flujo_entrada = new DataInputStream(auxi);

             flujo_salida.writeUTF("¡¡¡Conexión satisfactoria!!!");//Escribe en el Stream para que lo lea el cliente
             String comando;
             boolean correcto = false;
             flujo_salida.writeUTF("Introduce nombre del archivo: ");
             do {    
                 comando=flujo_entrada.readUTF();//Lectura de comando
                 if(comando.equals("dam")){
                     File archivo = new File ("dam.txt");
                     FileReader fr = new FileReader (archivo);
                     BufferedReader br = new BufferedReader(fr);
                     String linea = br.readLine();
                     flujo_salida.writeUTF("true");
                     flujo_salida.writeUTF(linea);
                     correcto = true;
                 }else{ 
                     flujo_salida.writeUTF("false");
                     correcto = false;
                 }
             } while (correcto == false);
             flujo_entrada.close();
             flujo_salida.close();
             System.out.println("Cliente desconectado");
             skCliente.close();
        } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

And the client

import java.io.*;
import java.net.*;
import java.util.*;

public class Cliente {

    public static void main(String[] args) throws IOException {

        final String ip = "localhost";
        final int port = 49000;
        String resultado = "false";

        Scanner sc = new Scanner(System.in);
        Socket sCliente = new Socket(ip, port);
        //System.out.println("Me he conectado al servidor");

        InputStream aux = sCliente.getInputStream();
        DataInputStream flujo_entrada = new DataInputStream(aux);

        OutputStream auxi = sCliente.getOutputStream();
        DataOutputStream flujo_salida = new DataOutputStream(auxi);

        System.out.println(flujo_entrada.readUTF());//Confirmacion
        System.out.print(flujo_entrada.readUTF());//Primera petición

        String archivo = sc.nextLine();
        flujo_salida.writeUTF(archivo);//Comando
        //resultado = flujo_entrada.readUTF();

        do{
            resultado = flujo_entrada.readUTF();
            if(resultado.equals("false")){
                System.out.println("No existe el archivo");
                System.out.print("Introduzca el nombre del archivo: ");
                archivo = sc.nextLine();
                flujo_salida.writeUTF(archivo); 
            }
            if(resultado.equals("true")){
                System.out.println("Lectura del archivo solicitado: ");
                System.out.println(flujo_entrada.readUTF());
                System.out.println(flujo_entrada.readUTF());
            }
        }while (resultado.equals("false")); 
        sCliente.close();
    }
}
    
answered by 19.02.2018 / 01:48
source