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);
}
}