Client Java Server Application is looped

0

I am developing a system for reinforcement school functions. The moment I went to implement my class that does the data encryption I needed to change also the type of data that will traffic between client and server from String to Byte. I already tried the encryption class is 100% working.

My problem is with the byte, when I run the client the screen it works normal until I send some data to the server, it stops works and stays static and does not display any error on the client and also on the server, I think it can be infinite loop because of the byte but I do not know how to recognize it.

class I use on the client and server:

public class Comunicador extends Thread {
    private String ip;
    private int port;
    private Socket sket;
    private BufferedOutputStream out;
    private BufferedInputStream in ;
    public boolean escutando = false;
    public String msg;

    public void setescutando(boolean escutando) {
        this.escutando = escutando;
    }

    public Comunicador(Socket sket) {
        this.sket = sket;
    }

    public Comunicador(String ip, int port) {
        this.ip = ip;
        this.port = port;

        this.conectar();
    }

    public void conectar() {
        try {
            this.sket = new Socket(this.ip, this.port);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Comunicador(int port) {
        this.port = port;

        this.iniciarServidor();
    }

    public void iniciarServidor() {
        try {
            ServerSocket server = new ServerSocket(this.port);
            this.sket = server.accept();
            server.close();
        } catch (IOException ex) {}
    }

    public void falar(String msg) throws Exception {
        criptografia cript = new criptografia();
        try {
            this.out = new BufferedOutputStream(this.sket.getOutputStream());
            out.write(cript.criptografa(msg));
            //out.close();
        } catch (IOException ex) {}
    }

    public String escutar() throws Exception {
        criptografia cript = new criptografia();
        try {
            //this.sket.request.connection.remoteAddress;
            this.in = new BufferedInputStream(this.sket.getInputStream());
            byte[] dataAsByte = new byte[20]; in .read(dataAsByte);
            String msg = cript.decriptografa(dataAsByte);
            //in.close();
        } catch (IOException ex) {}
        return msg;
    }


    public void desconectar() {
        try {
            this.sket.close();
            this.in.close();
            this.out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void run() {
        if (this.escutando) {
            try {
                this.msg = this.escutar();
                try {
                    this.callback();
                } catch (Exception ex) {
                    Logger.getLogger(Comunicador.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (Exception ex) {
                Logger.getLogger(Comunicador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void callback() throws Exception {
        //System.out.println(msg);
        Servidor servidor = new Servidor();
        this.msg = servidor.tomadaDeAcao(this.msg);
        this.falar(this.msg);
    }
}
    
asked by anonymous 21.02.2018 / 17:05

1 answer

0

A first comment is that the encryption class should start with a capital "C".

On the code presented there are a few things to consider:

  • The ideal is to have one class for the server and another for the client, this is because the server needs to execute accept on the Socket (as it has in its server start method)
  • It is very strange in your callback method to create a new server for each new action that is sent. This will create several objects in memory and I believe this is not the goal.
  • Although you are separating client and server via constructor overload this is totally unreadable.
  • It seems that every time an action is performed on the server for a client that has been connected it opens a new server object (which is not connected to that client) and this could generate this frozen system view.

        
    21.02.2018 / 19:15