停止Bufferedreader流程

时间:2013-03-01 04:01:10

标签: java bufferedreader

我的linux终端中的数据包捕获命令..

sudo tcpdump -w kbh-ns.pcap -i lo greater 106 and less 106

当我按

时,进程将停止并保存捕获的数据包
  

^ C

我有一个使用BufferedReader执行此操作的代码

public void SaveCapture() throws IOException
{
    List<String> command1 = new ArrayList<String>();
    //perintah untuk mecari koneksi ( SIP DIP SPort DPort )
    command1.clear();
    command1.add("sudo"); command1.add("tcpdump");
    command1.add("-w"); command1.add("kbh-ns.pcap"); //write
    command1.add("-i"); command1.add("vmnet1"); //interface
    command1.add("greater");command1.add("106"); //packet length
    command1.add("and");
    command1.add("less"); command1.add("106");//packet length
    command1.add("-c"); command1.add("20");

    ProcessBuilder pb = new ProcessBuilder(command1);
    process = pb.start();
    ProcessBuilder PB = new ProcessBuilder(command1);
    Process TerminalTask = PB.start();

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String s;
    s = stdInput.readLine();
}

如果我使用

,它会起作用
command1.add("-c"); command1.add("5");//number of packets to capture

我的排名: 如何使用java的^ c命令停止进程?

如果我使用process.destroy()

private void ButtonStopScanActionPerformed(java.awt.event.ActionEvent evt) {                                               
    if (process != null)
    {
        process.destroy();
        TxtAreaKet.setText(TxtAreaKet.getText() + "Complete.. \n");
    }
    end = System.currentTimeMillis();
    try {
        ReadPacket();
    } catch (IOException ex) {
        Logger.getLogger(Receiver1.class.getName()).log(Level.SEVERE, null, ex);
    }
    TxtAreaKet.setText(TxtAreaKet.getText() + "waktu : "+ ((end - start) / 1000.0) + " ms");        
} 

private void ReadPacket() throws IOException {
    List<String> command1 = new ArrayList<String>();
    //perintah untuk mecari koneksi ( SIP DIP SPort DPort )
    command1.clear();
    command1.add("sudo"); command1.add("tcpdump");
    command1.add("-r"); command1.add("kbh-ns.pcap"); //read
    command1.add("-n"); 

    ProcessBuilder pb = new ProcessBuilder(command1);
    process2 = pb.start();
    ProcessBuilder PB = new ProcessBuilder(command1);
    Process TerminalTask = PB.start();

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process2.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(process2.getErrorStream()));

    String s;
    long a = 0;
    while ((s = stdInput.readLine()) != null)
    {
        a++;
    }
    TxtAreaKet.setText(TxtAreaKet.getText() + "Captured Packet : " + a + "\n");
    while ((s = stdError.readLine()) != null)
    {
        TxtAreaKet.setText(TxtAreaKet.getText() + s + "\n");
    }
}

这并不能真正阻止我的过程,因为......

enter image description here

当我再次按Stop Scan

enter image description here

1 个答案:

答案 0 :(得分:0)

  

如何使用java的^ c命令停止进程?

只需致电Process.destroy()