0

I have wrote a program which run a batch command (tshark) to capture packet size between 2 ip address (continuously).

I used Runtime and Process to run it and process.getOutputStream() to get the returned values and print them in the java terminal.

My problem is that the print makes pauses between two records (print 1200 lines / stop 10 sec / print 1200 lines).

Do you know a way to read continuously, in the java app, the OutputStream of batch command ?

2 Answers2

0

First of all you want to read the InputStream: You read from an InputStream, you write to an OutputStream. See this post

Then use a BufferReader to wrap your OutputStream and read it line by line in a while loop.

You can also catch the error InputStream. It might be useful to debug if you don't see any output.

Here is an example with the ping command. As I don't know what your tsharp command is.

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ping www.google.com");

String line = null;

BufferedReader inputStreamReader = 
    new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = inputStreamReader.readLine()) != null) {
    System.out.println(line);
}

BufferedReader errorStreamReader = 
    new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((line = errorStreamReader.readLine()) != null) {
    System.out.println(line);
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
0

That's exactly my code. Ping comand end quickly so if i wait few seconds i have the result. Tshark capture network trafic so doesn't end.

My problem is that the reading make pauses.

This is my code :

    String cmd = "c:\\\"Program Files\"\\Wireshark\\tshark.exe -T fields -e frame.len host"+ ipSrc +" and dst "+ ipDst;

    String[] command = {"cmd.exe", "/C", cmd};

    try {
        final Process proc = Runtime.getRuntime().exec(command);
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(proc.getInputStream()));
            String line = "";
            try {
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } finally {
                reader.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

I also tried to use a Thread to write in terminal but it's the same problem :

    new Thread() {
            @Override
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(
                                    process.getInputStream()));
                    String line = "";
                    try {
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                    } finally {
                        reader.close();
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }.start();