开发者

output buffer empty until tcpdump is killed

I am running the tcpdump on my android emulator and since the tcpdump is running in the background there isnt any data in the buffer and hence the application is stuck at this point. here is the part of the code:

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;
              }

any help?


Your code logic is not good, since "BufferedInputStream.readLine()" is a blocking method.

So, in your code, you never exit from the while loop. So, you never reach the line "tv.setText(output);"

After reading a line from the process, you should print the buffer (and clean it), inside the while loop if you never want to close the stream or close the process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜