2010-09-08 57 views
0

我在我的android模拟器上运行tcpdump,并且由于tcpdump在后台运行,因此缓冲区中没有任何数据,因此应用程序停留在此位置。 这里是代码的一部分:输出缓冲区为空,直到tcpdump被杀死

else if (tcpdumpButton.isChecked()) 
      { 
       try 
       { 
       Process process1 = Runtime.getRuntime().exec("tcpdump"); 
       DataOutputStream os = new DataOutputStream(process1.getOutputStream()); 
       BufferedReader osRes = new BufferedReader(new InputStreamReader(process1.getInputStream())); 
       //ByteArrayInputStream osRes = (ByteArrayInputStream) process1.getInputStream(); 
       // os.writeBytes("tcpdump -l port 80"); 
       os.flush(); 
       StringBuffer output = new StringBuffer(); 
       try 
       { 
        while ((osRes.readLine()) != null) 
        { 
         output.append(osRes.readLine()); 
         output.append("\n"); 
        } 
       } 
       catch (Exception e) 
       { 
        throw e; 
       } 
       process1.waitFor(); 
       tv.setText(output); 
       setContentView(tv); 
       } 
       catch (Exception e) 
       { 
       throw e; 
       } 

任何帮助吗?

+0

您正在开始'tcpdump'子进程。什么使它终止? – erickson 2010-09-08 06:51:54

+0

我不希望它终止,因为我想捕获所有的数据包 – Jony 2010-09-08 06:57:13

回答

1

您的代码逻辑不好,因为“BufferedInputStream.readLine()”是一种阻塞方法。

因此,在你的代码中,你永远不会退出while循环。 所以,你永远不会到达“tv.setText(output);”

从流程中读取一行后,如果您不想关闭流或关闭进程,您应该在while循环内打印缓冲区(并清理它)