mq_receive:消息太长 [英] mq_receive: message too long

查看:769
本文介绍了mq_receive:消息太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用队列在2个进程之间实现通信.问题是,当我调用函数mq_receive时,出现此错误:消息太长.

我已经执行以下操作:

struct mq_attr attr;

long size = attr.mq_msgsize;
.... // initializing the queue "/gateway"

int rc = mq_receive(gateway, buffer, size, &prio);

如果我打印出大小值,则得到size = 1,而当我从另一个程序中打印出相同大小(通过相同的机制得到)时,得到的东西不是长整数(-1217186280)...

如何解决此错误?....因此,当size = 1时,我认为说消息太长"是正确的,但是为什么是1?

P.S.我也试过了:

int rc = mq_receive(gateway, buffer, sizeof(buffer), &prio);

但没有结果.

解决方案

似乎您需要更仔细地阅读文档.调用mq_receive时,应传递目标缓冲区的大小. 此大小必须大于队列的mq_msgsize属性.另外,似乎您在队列属性初始化中存在错误,导致无法正确进行mq_receive调用.这是标准的消息队列会话:

  1. 填充mq_attr结构( doc ):

    struct mq_attr attr;  
    attr.mq_flags = 0;  
    attr.mq_maxmsg = 10;  
    attr.mq_msgsize = 33;  
    attr.mq_curmsgs = 0;  
    

  2. 在主进程中使用mq_open创建队列( doc ):

    mqd_t queue = mq_open(qname, O_CREAT|O_RDWR, 0644, &attr);
    

  3. 在编写器进程中打开写队列:

    mqd_t queue = mq_open(qname, O_WRONLY);
    

    并发送一些文本.文本长度必须小于队列的mq_msgsize属性( doc ):

    mq_send(queue, "some message", strlen("some message")+1, 1);
    

  4. 在阅读器进程中,打开阅读队列:

    mqd_t queue = mq_open(qname, O_RDONLY);
    

    然后分配缓冲区并接收消息.缓冲区的大小*必须大于队列的mq_msgsize属性.在这里,我们创建50个字节的缓冲区,而mq_msgsize == 33( doc ):

    char rcvmsg[50];
    int iret = mq_receive(queue, rcvmsg, 50, NULL);
    

还请记住,应使用%ld打印long而不是%d.

I am implementing a communication between 2 processes using a queue. The problem is that when I call the function mq_receive, I get this error: Message too long.

I have done the following:

struct mq_attr attr;

long size = attr.mq_msgsize;
.... // initializing the queue "/gateway"

int rc = mq_receive(gateway, buffer, size, &prio);

If I print the size value, I get size=1, while when I print the same size but from another program (got by the same mechanism), I get something not long integer ( -1217186280 )...

How can I solve this error?....so while size = 1, I believe it's right to say "message too long" but why is 1?

P.S. I have also tried to put :

int rc = mq_receive(gateway, buffer, sizeof(buffer), &prio);

but with no result.

解决方案

It seems like you need to read the docs more carefully. When you call mq_receive you should pass size of the destination buffer. This size must be greater than the mq_msgsize attribute of the queue. In addition, it seems like you have an error in queue attributes initialisation that makes proper mq_receive call impossible. Here is standard message queue session:

  1. Fill mq_attr struct (doc):

    struct mq_attr attr;  
    attr.mq_flags = 0;  
    attr.mq_maxmsg = 10;  
    attr.mq_msgsize = 33;  
    attr.mq_curmsgs = 0;  
    

  2. Create queue with mq_open in master process (doc):

    mqd_t queue = mq_open(qname, O_CREAT|O_RDWR, 0644, &attr);
    

  3. In writer process open queue for writing:

    mqd_t queue = mq_open(qname, O_WRONLY);
    

    And send some text. Length of the text must be lesser than mq_msgsize attribute of the queue (doc):

    mq_send(queue, "some message", strlen("some message")+1, 1);
    

  4. In reader process open queue for reading:

    mqd_t queue = mq_open(qname, O_RDONLY);
    

    And then allocate buffer and receive message. Size of buffer *must be greater than the mq_msgsize attribute of the queue. Here we create 50-byte buffer while mq_msgsize == 33 (doc):

    char rcvmsg[50];
    int iret = mq_receive(queue, rcvmsg, 50, NULL);
    

Also remember that you should use %ld for print long instead of %d.

这篇关于mq_receive:消息太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆