linux - 在C中使用Popen调用tshark没有接收到输出
迷茫
迷茫 2017-04-17 14:36:54
[Linux讨论组]

在C中使用popen调用tshark,没有返回输出

例子代码如下:

//开启tshark
FILE *fp = popen("tshark -r t_fifo -T pdml", "r");

//读取popen输出
fread(buffer, 1024, 1, fp);

说明:

  1. t_fifo 为创建的FIFO,写入标准PCAP格式报文,用于tshark解析

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
阿神

1 man popen
可以看到
The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3). Writing to

   such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this
   is  altered by the command itself.  Conversely, reading from a "popened" stream reads the command's standard output, and the command's standard input is the
   same as that of the process that called popen().

也就是说,其返回值是新进程的STDOUT ; 如果新的进程没有在STDOUT 给出打印,你当然都不到;

2 在linutmint17 中执行
tshark -r t_fifo -T pdml
echo $?
为2;
说明命令不对,并且错误信息被重定向到STDERR了;

如果你向在STDOUT 中获得错误信息,可以使用
tshark -r t_fifo -T pdml 2>&1

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号