gpt4 book ai didi

java - 使用 Java 监视 tcpflow 输出?

转载 作者:行者123 更新时间:2023-11-30 10:55:24 25 4
gpt4 key购买 nike

如何使用 Java 监控 tcpflow 输出?我尝试了以下方法,但它不适用于 tcp 流 -c arg。

Process process;

try {
process = Runtime.getRuntime().exec("tcpflow -c");
InputStreamReader ir = new
InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {
System.out.println("Output: " + line);
}
} catch (IOException e) {
e.printStackTrace();
}

为什么 tcpflow 的输出没有被读取?但是,如果我执行 tcpflow -h,则会读取输出。

最佳答案

通过跟踪来自 tcpflow 的日志文件找到了解决方法。

public static void main(String[] args) {
File log = new File("packets.log");
PacketListener listener = new PacketListener();
Tailer tailer = Tailer.create(log, listener, 2500);
tailer.run();
}

以及跟踪它的类。

public class PacketListener extends TailerListenerAdapter {

@Override
public void handle(String line) {
System.out.println("Inbound: " + line);
}

}

关于java - 使用 Java 监视 tcpflow 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com