I am developing a program for reading packages sent by computers to a server that has the port 7779 enabled to receive this data. For this I use sockets from Java .
The problem I have is that I open the port, read the information, but after a moment it stops receiving the same. Apparently it is hanging, I know that data is still coming because I check it with tcpflow .
The code I use is the following.
public static void main(String[] args) {
iniciarArchivoLog();
iniciarConexionBaseDatos();
procesarTramas();
}
private static void procesarTramas(){
char caracter;
byte[] bytes;
InputStream stream = null;
int bytesLeidos;
Integer puerto;
ServerSocket serverSocket;
Socket socket;
String hexadecimal, cadena = "";
puerto = Integer.valueOf(obtenerPropiedad("socket.port"));
try {
serverSocket = new ServerSocket(puerto);
socket = serverSocket.accept();
stream = socket.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
bytesLeidos = 0;
bytes = new byte[1];
while(true){
try {
bytesLeidos = stream.read();
consolaLogger.info(String.valueOf(bytesLeidos));
caracter = convertirEnteroACaracter(bytesLeidos);
cadena += String.valueOf(caracter);
if(caracter == '@'){
consolaLogger.info(cadena);
cadena = "@";
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static char convertirEnteroACaracter(int valor){
char caracter;
caracter = (char) valor;
return caracter;
}
I use the read()
method to read the integer from stream
, and then convert it to character to print it, after some impressions, it does not print anymore.