我编写了一个程序,它运行批处理命令(tshark)来捕获2个ip地址(连续)之间的数据包大小。
我使用Runtime
和Process
来运行它,process.getOutputStream()
来获取返回的值并将它们打印在java终端中。
我的问题是打印件在两个记录之间暂停(打印1200行/停止10秒/打印1200行)。
您是否知道在Java应用程序中连续阅读批处理命令OutputStream
的方法?
答案 0 :(得分:0)
首先,您想要阅读InputStream
:您从 InputStream
读取,将写入 OutputStream
。 CloudBlockBlob.UploadFromByteArrayAsync
method
然后使用BufferReader
打包OutputStream
并在while循环中逐行读取。
您还可以捕获错误InputStream
。如果您没有看到任何输出,则调试可能很有用。
以下是ping
命令的示例。因为我不知道你的tsharp
命令是什么。
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);
}
答案 1 :(得分:0)
这正是我的代码。 Ping命令快速结束,如果我等了几秒钟,我就得到了结果。 Tshark捕获网络流量因此不会结束。
我的问题是阅读会暂停。
这是我的代码:
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();
}
我也尝试使用Thread在终端中写入,但它也是同样的问题:
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();