不停地监听和传递数据

问题描述

| 这段代码说明了我的问题:
//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().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{
}
如何在没有以下情况的情况下将数据函数传递给函数: 停止收听,直到其他情况为止; 继续将数据传递给功能,因此一旦发送数据,功能将能够接收更多信息。 就像一条链子一样,发送并准备立即接收!     

解决方法

        如果要在线程之间移动流数据,则应查看
PipedInputStream
PipedOutputStream
类。您的“ 3”会将其读取的数据写入PipedOutputStream,而您的其他线程将从链接的PipedInputStream读取数据。 当然,我不认为您的其他线程不会直接使用
p.getInputStream()
。     ,        尽管它不能回答您的特定问题,但您是否考虑过使用Java数据包捕获库,而不是尝试捕获派生的TShark的输出? 我发现JNetPCap非常易于使用。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...