ReadLine on TCPDump-Buffer is sometimes blocked until tcpdump is killed

I have a problem using TCPDump from my Android application. It should read tcpdump output in turn and process it in my application.
Problem:
Sometimes the code works fine, it immediately reads the captured packets. But sometimes ReadLine blocks block the tcpdump process from the Linux console (killall tcpdump). After that, my loop is processed for each line (sometimes 10, sometimes 1 or 2), which means that readLine should have worked, but it hasn’t.
I read about similar problems, but did not find a solution for this problem ...
THANKS !!

public class ListenActivity extends Activity {

static ArrayList<Packet> packetBuffer = new ArrayList<Packet>();
static Process tcpDumpProcess = null;
static ListenThread thread = null;
public static final String TCPDUMP_COMMAND = "tcpdump -A -s0 | grep -i -e 'Cookie'\n";

private InputStream  inputStream = null;
private OutputStream outputStream = null;

@Override
protected void onStart() {
    super.onStart();
    try {
        tcpDumpProcess = new ProcessBuilder().command("su").redirectErrorStream(true).start();
        inputStream = tcpDumpProcess.getInputStream();
        outputStream = tcpDumpProcess.getOutputStream();
        outputStream.write(TCPDUMP_COMMAND.getBytes("ASCII"));
    } catch (Exception e) {
        Log.e("FSE", "", e);
    }
    thread = new ListenThread(new BufferedReader(new InputStreamReader(inputStream)));
    thread.start();
}

private class ListenThread extends Thread {

    public ListenThread(BufferedReader reader) {
        this.reader = reader;
    }

    private BufferedReader reader = null;

    @Override
    public void run() {

        reader = new BufferedReader(new InputStreamReader(inputStream));
        while (true) {
            try {                   
                String received = reader.readLine();
                Log.d("FS", received);
                Packet pReceived = Packet.analyze(received);
                if (pReceived != null) {
                    packetBuffer.add(pReceived);
                }
            } catch (Exception e) {
                Log.e("FSE", "", e);
            }

        }

    }

}

}

+1
source share
2 answers

, , , tcpdump grep , , . , , , ( setvbuf(3), ):

tcpdump(8):

   -l     Make stdout line buffered.  Useful if you want to see
          the data while capturing it.  E.g.,
          ``tcpdump  -l  |  tee dat'' or ``tcpdump  -l   >
          dat  &  tail  -f  dat''.

grep(1):

   --line-buffered
          Use line buffering on output.  This can cause a
          performance penalty.

:

"tcpdump -l -A -s0 | grep --line-buffered -i -e 'Cookie'\n";
+2

, , -l , , tcpdump. , TcpDump . TcpDump :

tcpdump -l-A -s0 > /data/local/output.txt

run :

File dumpedFile = new File("/data/local/output.txt");
//open a reader on the tcpdump output file
BufferedReader reader = new BufferedReader(new FileReader(dumpedFile));
String temp = new String();
//The while loop is broken if the thread is interrupted   
while (!Thread.interrupted()) {    
    temp = reader.readLine();
    if (temp!=null) {
        Log.e("READER",new String(temp));    
    }
}

, grep, , Java.

, TcpDump , , ​​ . , , / tcpdump.

0

All Articles