首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命名管道仅在读取而不在写入

命名管道仅在读取而不在写入
EN

Stack Overflow用户
提问于 2016-05-11 14:03:22
回答 1查看 47关注 0票数 0

我有一个用来读入二进制数据的InPipe和一个用来写回通过防火墙的二进制数据的OutPipe。

代码语言:javascript
复制
/// The input named pipe, "ToFirewall"
static FILE* InPipe = NULL;


/// The output named pipe, "FromFirewall"
static FILE* OutPipe = NULL;

我在一个单独的函数中打开两个管道。

代码语言:javascript
复制
static bool OpenPipes(void)
{
    //ToFirewall
    InPipe = fopen("ToFirewall", "rb");
    if(InPipe == NULL)
    {
       perror("ERROR, failed to open pipe ToFirewall:");
       return false;
    }

   OutPipe = fopen("FromFirewall", "wb");
   if(OutPipe == NULL)
   {
      perror("ERROR, failed to open pipe FromFirewall:");

           return false;
       }

   return true;
}

由于某些原因,当我读取数据时,它会跳过写入,而不会费心检查它是否通过了我的防火墙。我在网上看过关于刷新的解决方案,但它没有帮助。

代码语言:javascript
复制
static void* FilterThread(void* args) {
    OpenPipes();

    unsigned char* buffer = malloc(1500);

    int ret = fread(buffer, 1, 1500, InPipe);
    if(ret){
        fclose(InPipe);
    }


    //Check is FilterPacket will allow the packet through the firewall
    if(FilterPacket(buffer, args)) {
        fwrite(buffer, 1, 60, OutPipe);
        fflush(OutPipe);
    }

//        fflush(OutPipe);
    fclose(OutPipe);

    return NULL;
}

这是我的输出

代码语言:javascript
复制
RCVR: opened file output.bin
SNDR: Waiting 200ms between packets
SNDR: Number of packets: 18
SNDR: Starting packet 0
SNDR: Starting packet 1
SNDR: Starting packet 2
SNDR: Starting packet 3
SNDR: Starting packet 4
SNDR: Starting packet 5
SNDR: Starting packet 6
SNDR: Starting packet 7
SNDR: Starting packet 8
SNDR: Starting packet 9
SNDR: Starting packet 10
SNDR: Starting packet 11
SNDR: Starting packet 12
SNDR: Starting packet 13
SNDR: Starting packet 14
SNDR: Starting packet 15
SNDR: Starting packet 16
SNDR: Starting packet 17
SNDR: Finished, wrote 18 packets to the pipe

在这里,您可以看到预期的输出实际应该是什么样子

代码语言:javascript
复制
> RCVR: opened file output.bin
SNDR: Waiting 200ms between packets
SNDR: Number of packets: 18
SNDR: Starting packet 0
SNDR: Starting packet 1
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 2
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 3
SNDR: Starting packet 4
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 5
SNDR: Starting packet 6
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 7
RCVR: 129.21.37.28 -> 74.125.21.103
SNDR: Starting packet 8
SNDR: Starting packet 9
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 10
RCVR: 74.125.21.103 -> 129.21.37.28
SNDR: Starting packet 11
SNDR: Starting packet 12
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 13
RCVR: 129.21.37.28 -> 74.125.21.103
SNDR: Starting packet 14
SNDR: Starting packet 15
SNDR: Starting packet 16
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 17
RCVR: 74.125.21.103 -> 129.21.37.28
SNDR: Finished, wrote 18 packets to the pipe
FwSim, Commanding firewall to Exit
RCVR: 74.125.21.103 -> 129.21.37.11
Exiting
EN

回答 1

Stack Overflow用户

发布于 2016-05-11 17:38:34

读操作最多读取1500字节的数据,但您只写入了60字节。您可能想要修复此问题:

代码语言:javascript
复制
fwrite(buffer, 1, ret, OutPipe);

并且只有在检查过滤器返回true时才会发生写操作,所以为了帮助调试,我建议在过滤器返回false时添加日志:

代码语言:javascript
复制
if(FilterPacket(buffer, args)) {
    fwrite(buffer, 1, ret, OutPipe);
    fflush(OutPipe);
} else {
    fprintf(stderr, "FilterPacket returned false for packet %s\n", buffer);
}

另外,当函数返回时,buffer不会被释放,这将导致内存泄漏,请在函数的末尾添加以下内容:

代码语言:javascript
复制
free(buffer);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37154227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档