2012-01-13 14 views
5

Escribo un chat simple en Java, y quiero verificar si hay algunos datos esperando en BufferedReader. He leído sobre NIO, pero no lo entendí del todo. Aquí es parte de mi código:Verificar datos entrantes en Java Socket

public void Send(String data) 
{ 
    out.println(data); 
} 

public String Recv() 
{ 
    if (dataIncomming) 
    { 
     try { 
      return in.readLine(); 
     } catch (IOException e) { 
      System.err.println("Send: Error on BufferedReader.readLine() - IOException"); 
     } 
    } 
    else return ""; 
} 

No sé qué llenar en dataIncomming ...

Respuesta

7

utilizar el método de Stream.Available(). También es posible que desee esperar hasta que se reciba la cantidad correcta de bytes y espere para que el subproceso no se ejecute el 100% del tiempo.

while(Stream.Available() != 0); //block until there is data 

try{ 
    return in.readLine(); 
} catch (IOException e) { 
    System.err.println("Send: Error on BufferedReader.readLine() - IOException"); 
} 
Cuestiones relacionadas