Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将pcap_t*文件描述符分配给内存块?_C_Pcap_File Descriptor - Fatal编程技术网

是否将pcap_t*文件描述符分配给内存块?

是否将pcap_t*文件描述符分配给内存块?,c,pcap,file-descriptor,C,Pcap,File Descriptor,我编写了一个C程序,它将一个有效的PCAP文件写入一个malloc'ed内存块: u_char* myPCAP = writePCAP( ... ); 这个程序运行得非常好,如果我把myPCAP写入一个文件,我就可以在Wireshark中读取该文件以及所有内容。所以我知道一切都在运转。 但是现在,我想为myPCAP分配一个pcap\u t*文件描述符,并将该FD传递给另一个程序。(nDPI,对于那些好奇的人)我该怎么做?我希望这能奏效: pcap_t* pcap = fdopen( ((int

我编写了一个C程序,它将一个有效的PCAP文件写入一个malloc'ed内存块:

u_char* myPCAP = writePCAP( ... );
这个程序运行得非常好,如果我把myPCAP写入一个文件,我就可以在Wireshark中读取该文件以及所有内容。所以我知道一切都在运转。 但是现在,我想为myPCAP分配一个
pcap\u t*
文件描述符,并将该FD传递给另一个程序。(nDPI,对于那些好奇的人)我该怎么做?我希望这能奏效:

pcap_t* pcap = fdopen( ((int*)myPCAP), "r" );
但是:


有人看到解决办法了吗?一定有办法的,对吧?谢谢。

fdopen()的参数必须是来自内核的文件描述符。文件描述符不是指针。不能将内存视为文件描述符。你为什么不用烟斗?@Barmar我愿意接受任何解决方案。可以在C程序中使用管道吗?可以,使用
pipe()
系统调用。
me@ubuntu:/home/me#  make all
gcc -g -fPIC -DPIC -I../src/include -g -O2 -c MyCode.c -o MyCode.o
MyCode.c: In function ‘msgHandler’:
MyCode.c:750:34: warning: passing argument 1 of ‘fdopen’ makes integer from pointer without a cast [-Wint-conversion]
  pcap_t* pcap = (pcap_t*)fdopen( ((int*)myPCAP), "r" );
                                  ^
In file included from MyCode.c:16:0:
/usr/include/stdio.h:265:14: note: expected ‘int’ but argument is of type ‘int *’
 extern FILE *fdopen (int __fd, const char *__modes) __THROW __wur;
              ^~~~~~
g++ -g -fPIC -DPIC -I../src/include -g -O2 MyCode.o libndpiReader.a -o ndpiReader ../src/lib/libndpi.a -lpcap  -lpthread -lm
me@ubuntu:/home/me#