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 oneif
inside, which after finishing, kills the corresponding thread. How do you use or expect to use this class?– Victor Stafusa