2012-06-04 165 views
-2

我正在开发基于线程异步套接字客户端。当程序调用readLine(),它会阻止无限,永不返回。Asyncrhonous套接字客户端

public class ADNClient { 

    Socket socket = null; 
    DataOutputStream dataOutputStream = null; 
    DataInputStream dataInputStream = null; 

    Thread listener = new Thread(new Runnable() { 
    @Override 
    public void run() { 
     String line; 
     try { 
     // Stop here and doesn't progress 
     while ((line = dataInputStream.readLine()) != null) { 
      //DO something 
     } 
     } 
     catch (IOException e) {} 
    }); 

    public ADNClient() { 
    try { 
     socket = new Socket("192.168.1.5", 5000); 
     dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
     dataInputStream = new DataInputStream(socket.getInputStream()); 
     listener.start(); 
     //sender.start(); 
    } catch (Exception e) { 
     Log.e("ADN", e.getMessage()); 
    } 
    } 

    public void close() { 
    listener.stop(); 
    try { 
     socket.close(); 
    } catch (IOException e) { 
     Log.e("ADN", e.getMessage()); 
    } 
    } 
} 
+2

哪些数据您发送客户端?难道是用换行符终止? –

回答

0

好吧...我小白与IN/Outputstreams ...使用

readUTF() 

,而不是

我没有发送换行符,以recive信息的正确方法
readLine() 

谢谢格雷格Kopff!