开发者

Listening and passing data without stopping

This code illustrates my problem:

//Constructor
public ReadFormat(String path, int nic) throws IOException{
     this.path = path;
     this.nic = String.valueOf(nic);        
    ReadTshark();    //Calls listening function
}

//listening stdout and sends to another
public void ReadTshark() throws IOException{
    String cmdTshark = path + " -S" + " -i "+ nic + " -R "+ "\"udp.port == 9000\" " + "-E header=y -E separator=, -Tfields -e frame.len -e frame.time_delta";  
    System.out.println(cmdTshark);
    String s=null;
    Process p = Runtime.getRuntime().开发者_JAVA技巧exec(cmdTshark);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read the output from the command
    int i=0;
    while ((s = stdInput.readLine()) != null && b) {
            System.out.println(s);
            rawin[i]=s;
            if(i==100){
                Separate();    //When rawin reaches 100, sends to another function to process    
            }
        }
}

//receives data and sends to another function
private void Separate(){
}
//receives data and sends to another function
private void NextFunction{
}

How can I pass the data function to function without:

  1. Stop listening until otherwise;
  2. Continue to pass data on to functions, so as soon the data is sent the function will be able to receive more.

Just like a chain, send and ready to receive immediately!


if you want to move stream data between threads, you should look at the PipedInputStream and PipedOutputStream classes. your ReadTshark() would write the data it reads to the PipedOutputStream, and your other thread would read the data from a linked PipedInputStream.

of course, i see no reason why your other thread would not just use the p.getInputStream() directly.


Although it doesn't answer your specific question, have you considered using a Java packet capture library rather than trying to capture the output from a forked TShark?

I find JNetPCap quite easy to use.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜