Hilo que devuelve variable repetida
Publicado por Reinaldo (36 intervenciones) el 09/01/2007 20:38:06
Hola.
Tengo el siguiente problema, me conecto via telnet a un msc y me devuelve cierta informacion. Pero cada vez que ejecuto el metodo desde la interfaz grafica el String de retorno se acumula y me devuelve la primera busqueda + la segunda busqueda (cosa que no quiero), no se si es el buffer que se queda lleno u otra cosa, he probado limpiando la variable pero nada.
Adjunto les coloco el codigo a ver si alguien tiene la solucion o alguna idea.
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
public class Basico_Telnet
{
//ListenToClient thread listenes to the remote host continuously
ListenToClient lt;
BufferedReader br;
PrintWriter pr;
Socket s;
String prevString;
static Constantes cns = new Constantes();
static Comandos cmd = new Comandos();
static Bd db = new Bd();
String miFecha="";
public static String numGlobal="";
public static String result= "0";
public static String ubica="";
public static String numAseguir="";
public String direxion="";
public static void main(String [] args)
{
Basico_Telnet t = new Basico_Telnet();
result= t.connect("10.38.30.100","23","584125975236");
System.out.println(result);
System.out.println(numGlobal);
}
public String connect(String host, String portNumber, String GSM)
{
int port;
int i =1;
numAseguir=GSM;
System.out.println("numAseguir "+numAseguir);
prevString="";
if(host.length() < 1 || portNumber.length() < 1)
{
System.out.println("Puerto o direccion ip invalidos");
return("-1");
}
try
{
prevString = new String("");
port = Integer.parseInt(portNumber);
s = new Socket(host, port);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pr = new PrintWriter(s.getOutputStream(), true);
pr.println(cns.USERCARACAS1 + cmd.BARRA_N);
pr.flush();
if (host.compareTo(cns.IPVALENCIA)==0) // es valencia
{pr.println(cns.PASSWORDVALENCIA + cmd.BARRA_N);} // // A pr se le asigna el password de Valencia
else // caracas1, caracas2 o caracas3
{pr.println(cns.PASSWORDCARACAS1 + cmd.BARRA_N);} // a pr se le asigna el Password de caracas1, 2, 3
pr.flush();
pr.println(cmd.COMANDO_CELDA + GSM + cmd.BARRAP_N);
pr.flush();
pr.println(cmd.COMANDO_Z + cmd.BARRA_N);
pr.flush();
pr.println(cmd.COMANDO_Z + cmd.BARRA_N);
pr.flush();
lt=new ListenToClient();
lt.start();
while (lt.receivedTmp.equals(" ")){
{i++;} }
s.close();
lt.listening = false;
if(lt.isAlive() == true)
lt.interrupt();
try{
Thread.sleep(3000);
}
catch (InterruptedException e)
{System.out.println(e);
}
direxion=lt.receivedTmp;
System.out.println("DIREXION "+direxion);
return (lt.receivedTmp);
}
catch(NumberFormatException e)
{
return("-1");
}
catch(IOException e)
{
return("-1");
}
}
class ListenToClient extends Thread
{
boolean listening;
String received;
public String receivedTmp = " ";
public String stringProcesamiento=" ";
String line2;
public ListenToClient()
{
receivedTmp = " ";
listening = true;
}
public void run()
{
while(listening)
{
try
{
received = new String(br.readLine());
prevString= " ";
prevString = new String(prevString + received + cmd.BARRA_N);
if (lt.received.indexOf(cmd.ULTIMA_CELDA) != -1)
{
try {
stringProcesamiento = String.valueOf(Integer.parseInt(lt.received.substring(lt.received.indexOf("/") + 1 , lt.received.indexOf("/") + 6)));
stringProcesamiento = stringProcesamiento.trim();
File File2 = new File(cmd.ARCHIVO_CELDAS);
FileReader FR2 = new FileReader(File2);
BufferedReader BR2 = new BufferedReader(FR2);
while ( (line2 = BR2.readLine()) != null)
{
if (line2.indexOf("#" + stringProcesamiento + "#")!= -1)
receivedTmp= receivedTmp + " " + "Ubicacion:" + line2;
}
//Se busca en BD la información precisa de la Celda
//System.out.println("LA CELDA ES : "+ stringProcesamiento);
// laUbicacion=db.buscandoCelda(stringProcesamiento);
//System.out.println("NUMERO DE LA CELDA"+stringProcesamiento);
//receivedTmp= receivedTmp + " " + "Ubicacion:" + laUbicacion;
receivedTmp= receivedTmp + " " + "Ubicacion: " ;//+ direccion;
ubica=receivedTmp;
receivedTmp= receivedTmp.trim() + "Celda #:" + stringProcesamiento.trim();
receivedTmp= receivedTmp.trim();
numGlobal=stringProcesamiento;
stringProcesamiento = "";
BR2.close();
FR2.close();
}
// Si da error de formato cuando se trata de convertir a numero la celda para buscarla en el archivo,
//se indica que el suiche esta no está arrojando la celda
catch (NumberFormatException f)
{receivedTmp= receivedTmp.trim() + " Celda: No Indicada por el Switch";
}
/*catch (IOException e)
{
System.out.println("Ha ocurrido una excepción del tipo: " + e);
}*/
}
else if (lt.received.indexOf(cmd.ULTIMA_ACTIVACION)!= -1)
{
String strReturn="";
// como lt.received tiene la linea del telnet donde está la fecha,
//entonces a string procesamiento se le va a asignar exactamente la fecha MM-DD HH:MM
stringProcesamiento = lt.received.substring(lt.received.indexOf(":") - 8 , lt.received.indexOf(":") + 3);
miFecha= stringProcesamiento;
receivedTmp= receivedTmp + " " + "Fecha:" + miFecha + " " + ubica;
receivedTmp= receivedTmp.trim();
stringProcesamiento = "";
}
else if (lt.received.indexOf(cmd.DETACH)!= -1)
{
// como lt.received tiene la linea del telnet donde está la Causa de desconexión,
// Y posee un EIAF entonces a stringprocesamiento se le va a agregar "Telefono apagado"
System.out.println("telefono apagado o fuera del area de cobertura");
receivedTmp= receivedTmp + "Motivo: Desconexión";
stringProcesamiento = "";
}
else if (lt.received.indexOf(cmd.DESCONOCIDO)!= -1 )
{ receivedTmp= "TELEFONO NO ENCONTRADO"; }
}
catch(IOException e)
{
//System.out.println("Connection closed.");
}
catch(NullPointerException e)
{
listening = false;
}
}
}
}
}
Tengo el siguiente problema, me conecto via telnet a un msc y me devuelve cierta informacion. Pero cada vez que ejecuto el metodo desde la interfaz grafica el String de retorno se acumula y me devuelve la primera busqueda + la segunda busqueda (cosa que no quiero), no se si es el buffer que se queda lleno u otra cosa, he probado limpiando la variable pero nada.
Adjunto les coloco el codigo a ver si alguien tiene la solucion o alguna idea.
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
public class Basico_Telnet
{
//ListenToClient thread listenes to the remote host continuously
ListenToClient lt;
BufferedReader br;
PrintWriter pr;
Socket s;
String prevString;
static Constantes cns = new Constantes();
static Comandos cmd = new Comandos();
static Bd db = new Bd();
String miFecha="";
public static String numGlobal="";
public static String result= "0";
public static String ubica="";
public static String numAseguir="";
public String direxion="";
public static void main(String [] args)
{
Basico_Telnet t = new Basico_Telnet();
result= t.connect("10.38.30.100","23","584125975236");
System.out.println(result);
System.out.println(numGlobal);
}
public String connect(String host, String portNumber, String GSM)
{
int port;
int i =1;
numAseguir=GSM;
System.out.println("numAseguir "+numAseguir);
prevString="";
if(host.length() < 1 || portNumber.length() < 1)
{
System.out.println("Puerto o direccion ip invalidos");
return("-1");
}
try
{
prevString = new String("");
port = Integer.parseInt(portNumber);
s = new Socket(host, port);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pr = new PrintWriter(s.getOutputStream(), true);
pr.println(cns.USERCARACAS1 + cmd.BARRA_N);
pr.flush();
if (host.compareTo(cns.IPVALENCIA)==0) // es valencia
{pr.println(cns.PASSWORDVALENCIA + cmd.BARRA_N);} // // A pr se le asigna el password de Valencia
else // caracas1, caracas2 o caracas3
{pr.println(cns.PASSWORDCARACAS1 + cmd.BARRA_N);} // a pr se le asigna el Password de caracas1, 2, 3
pr.flush();
pr.println(cmd.COMANDO_CELDA + GSM + cmd.BARRAP_N);
pr.flush();
pr.println(cmd.COMANDO_Z + cmd.BARRA_N);
pr.flush();
pr.println(cmd.COMANDO_Z + cmd.BARRA_N);
pr.flush();
lt=new ListenToClient();
lt.start();
while (lt.receivedTmp.equals(" ")){
{i++;} }
s.close();
lt.listening = false;
if(lt.isAlive() == true)
lt.interrupt();
try{
Thread.sleep(3000);
}
catch (InterruptedException e)
{System.out.println(e);
}
direxion=lt.receivedTmp;
System.out.println("DIREXION "+direxion);
return (lt.receivedTmp);
}
catch(NumberFormatException e)
{
return("-1");
}
catch(IOException e)
{
return("-1");
}
}
class ListenToClient extends Thread
{
boolean listening;
String received;
public String receivedTmp = " ";
public String stringProcesamiento=" ";
String line2;
public ListenToClient()
{
receivedTmp = " ";
listening = true;
}
public void run()
{
while(listening)
{
try
{
received = new String(br.readLine());
prevString= " ";
prevString = new String(prevString + received + cmd.BARRA_N);
if (lt.received.indexOf(cmd.ULTIMA_CELDA) != -1)
{
try {
stringProcesamiento = String.valueOf(Integer.parseInt(lt.received.substring(lt.received.indexOf("/") + 1 , lt.received.indexOf("/") + 6)));
stringProcesamiento = stringProcesamiento.trim();
File File2 = new File(cmd.ARCHIVO_CELDAS);
FileReader FR2 = new FileReader(File2);
BufferedReader BR2 = new BufferedReader(FR2);
while ( (line2 = BR2.readLine()) != null)
{
if (line2.indexOf("#" + stringProcesamiento + "#")!= -1)
receivedTmp= receivedTmp + " " + "Ubicacion:" + line2;
}
//Se busca en BD la información precisa de la Celda
//System.out.println("LA CELDA ES : "+ stringProcesamiento);
// laUbicacion=db.buscandoCelda(stringProcesamiento);
//System.out.println("NUMERO DE LA CELDA"+stringProcesamiento);
//receivedTmp= receivedTmp + " " + "Ubicacion:" + laUbicacion;
receivedTmp= receivedTmp + " " + "Ubicacion: " ;//+ direccion;
ubica=receivedTmp;
receivedTmp= receivedTmp.trim() + "Celda #:" + stringProcesamiento.trim();
receivedTmp= receivedTmp.trim();
numGlobal=stringProcesamiento;
stringProcesamiento = "";
BR2.close();
FR2.close();
}
// Si da error de formato cuando se trata de convertir a numero la celda para buscarla en el archivo,
//se indica que el suiche esta no está arrojando la celda
catch (NumberFormatException f)
{receivedTmp= receivedTmp.trim() + " Celda: No Indicada por el Switch";
}
/*catch (IOException e)
{
System.out.println("Ha ocurrido una excepción del tipo: " + e);
}*/
}
else if (lt.received.indexOf(cmd.ULTIMA_ACTIVACION)!= -1)
{
String strReturn="";
// como lt.received tiene la linea del telnet donde está la fecha,
//entonces a string procesamiento se le va a asignar exactamente la fecha MM-DD HH:MM
stringProcesamiento = lt.received.substring(lt.received.indexOf(":") - 8 , lt.received.indexOf(":") + 3);
miFecha= stringProcesamiento;
receivedTmp= receivedTmp + " " + "Fecha:" + miFecha + " " + ubica;
receivedTmp= receivedTmp.trim();
stringProcesamiento = "";
}
else if (lt.received.indexOf(cmd.DETACH)!= -1)
{
// como lt.received tiene la linea del telnet donde está la Causa de desconexión,
// Y posee un EIAF entonces a stringprocesamiento se le va a agregar "Telefono apagado"
System.out.println("telefono apagado o fuera del area de cobertura");
receivedTmp= receivedTmp + "Motivo: Desconexión";
stringProcesamiento = "";
}
else if (lt.received.indexOf(cmd.DESCONOCIDO)!= -1 )
{ receivedTmp= "TELEFONO NO ENCONTRADO"; }
}
catch(IOException e)
{
//System.out.println("Connection closed.");
}
catch(NullPointerException e)
{
listening = false;
}
}
}
}
}
Valora esta pregunta


0