Java Server Application Client loops

Asked

Viewed 72 times

0

I’m developing a system for school reinforcement simple functions. At the time I went to implement my class that makes the data encryption needed to change also the type of data that will traffic between client and String server to Byte. I have tested the encryption class and is 100% working.

My problem is with byte, when I run client to screen works normal until I send some data to the server, it stops works and stays static and shows no error nor in the client and also on the server, I think it might be infinite loop on account of the byte but I can’t recognize.

class I use on 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);
    }
}
  • This class seems to me very strange, especially due to the fact of the method run only have one if inside, which after finishing, kills the corresponding thread. How do you use or expect to use this class?

1 answer

0

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

About the code presented there are some things to consider:

  1. The ideal is to have one class for the server and another for the client, because the server needs to run Accept in Socket (as it has in its method start)
  2. It is very strange in your callback method you create a new server for each new action that is sent. This will create several objects in memory and I believe that is not the goal.
  3. Although you are separating client and server via constructor overhead this is totally unreadable.

It seems every time an action is executed on the server for a client that has been connected it opens a new server object (which is not connected on that client) and this could generate this frozen system view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.