mq_receive: message too long

前端 未结 4 980
再見小時候
再見小時候 2020-12-31 11:20

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 h

4条回答
  •  误落风尘
    2020-12-31 11:40

    When debugging realtime POSIX queues, you should start with a sample program which works and go forward from there. Once you have the sample program running, it should be a simple matter of ensuring that your own code follows all the steps.

    The following program has been tested successfully under Ubuntu 11.04:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define MQNAME "/pax"
    #define MQMESG "Hello there!"
    
    static mqd_t serverUp (void) {
        int rc;
        mqd_t svrHndl;
        struct mq_attr mqAttr;
    
        printf ("Bringing up server.\n");
        rc = mq_unlink (MQNAME);
        if (rc < 0) {
            printf ("   Warning %d (%s) on server mq_unlink.\n",
                errno, strerror (errno));
        }
    
        mqAttr.mq_maxmsg = 10;
        mqAttr.mq_msgsize = 1024;
        svrHndl = mq_open (MQNAME, O_RDWR|O_CREAT, S_IWUSR|S_IRUSR, &mqAttr);
        if (svrHndl < 0) {
            printf ("   Error %d (%s) on server mq_open.\n",
                errno, strerror (errno));
            exit (1);
        }
        printf ("   Server opened mqd_t of %d.\n", svrHndl);
        return svrHndl;
    }
    
    static void serverReceive (mqd_t svrHndl) {
        int rc;
        char buffer[2048];
        printf ("Server receiving on mqd_t %d.\n", svrHndl);
        rc = mq_receive (svrHndl, buffer, sizeof (buffer), NULL);
        if (rc < 0) {
            printf ("   Error %d (%s) on server mq_receive.\n",
                errno, strerror (errno));
            exit (1);
        }
        printf ("   Received [%s].\n", buffer);
    }
    
    static void serverDown (mqd_t svrHndl) {
        printf ("Bringing down server with mqd_t %d.\n", svrHndl);
        mq_close (svrHndl);
    }
    static void clientSend (void) {
        mqd_t cliHndl;
        int rc;
        printf ("Client sending.\n");
        cliHndl = mq_open (MQNAME, O_RDWR);
        if (cliHndl < 0) {
            printf ("   Error %d (%s) on client mq_open.\n",
                errno, strerror (errno));
            exit (1);
        }
        printf ("   Client opened mqd_t of %d.\n", cliHndl);
    
        rc = mq_send (cliHndl, MQMESG, sizeof (MQMESG), 1);
        if (rc < 0) {
            printf ("   Error %d (%s) on client mq_send.\n",
                errno, strerror (errno));
            exit (1);
        }
    
        mq_close (cliHndl);
    }
    
    int main (void) {
        mqd_t svrHndl;
    
        svrHndl = serverUp ();
        clientSend ();
        serverReceive (svrHndl);
        serverDown (svrHndl);
    
        return 0;
    }
    

    The output on my system is:

    Bringing up server.
       Server opened mqd_t of 3.
    Client sending.
       Client opened mqd_t of 4.
    Server receiving on mqd_t 3.
       Received [Hello there!].
    Bringing down server with mqd_t 3.
    

提交回复
热议问题