Hello,

I tried to compile a posix mq_send and mq_receive application. The msg is sent on the queue successfully, but during the receive part, an error occurs. I have attached the send and receive part of the code, please do suggest any corrections.

mq_send

#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

#define HI_PRIO 31
#define MSG_SIZE 16
//#define MSG "test"   
#define MQ_NAME "/msgqueue"
int main(void)
{
	    mqd_t    mqPXId;          /* msg queue descriptor */
		char *msg = "test";
     	
        /* open msg queue; should already exist with default attributes */ 
        if ((mqPXId = mq_open (MQ_NAME, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL)) ==-1) 
            { 
            printf ("sendTask: mq_open failed: %s\n", strerror(errno)); 
            return 0; 
            }

     
       /* try writing to queue */ 
        if (mq_send (mqPXId, msg, MSG_SIZE, HI_PRIO) == -1) 
            { 
            printf ("sendTask: mq_send failed\n"); 
            return 0; 
            } 
        else 
		{
            printf ("sendTask: mq_send succeeded, msg sent: %s\n",msg); 
		 }
			return 0;
}

mq_receive

#include <stdio.h>
#include <mqueue.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#define MSG_SIZE 16
#define MQ_NAME "/msgqueue"

int main(void)
{
    mqd_t    mqPXId;         /* msg queue descriptor */ 
        char     *msg;  /* msg buffer */ 
        unsigned int      prio;           /* priority of message */

     
        /* open message queue using default attributes */ 
        if ((mqPXId = mq_open (MQ_NAME, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL))  
            ==  -1) 
            { 
            printf ("receiveTask: mq_open failed\n"); 
            return 0; 
            }

     
        /* try reading from queue */ 
        if (mq_receive (mqPXId,(char *)&msg,MSG_SIZE, &prio) == -1) 
            { 
            printf ("receiveTask: mq_receive failed: %s\n",strerror(errno)); 
            return 0; 
            } 
        else 
            { 
            printf ("receiveTask: Msg of priority %d received:\n\t\t%s\n", 
                    prio, msg); 
            } 
return 0;
        }

It would be better if you actually posted your actual error message.

Rather than saying "an error", hoping we can be
a) bothered to run your code
b) actually end up with the same error message
Neither of these is guaranteed.

Sorry about that. The error that i receive is in the "mq_receive" section that states "message is too long". The data that I am passing to the queue is not large and the msg_size is also sufficient.

It would be better if you actually posted your actual error message.

Rather than saying "an error", hoping we can be
a) bothered to run your code
b) actually end up with the same error message
Neither of these is guaranteed.

Try to call mq_getattr function and check current number of bytes in queue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.