Hello everyone,

While implementing POSIX messaging queue, I am encountered with "Message too long error". I have stated 128 bits for my problem, but it still gives the same error. Please do help me in resolving this memory issue.

Regards,

Tapan.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <mqueue.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
struct test
{
	int a;
	char *b;
	char *c;
};

void testrecv(mqd_t mqdes2)
{
	struct test *testptr;
	testptr=(struct test *)malloc(sizeof(struct test));
	if(mq_receive(mqdes2, (char *)&testptr, 128, NULL)==-1)
	{
		printf("Queue receive failed\n");
		printf("Error: %s\n", strerror(errno));
	}
	printf("Leaving Test Receive\n");
	free(testptr);
}

void testsend(mqd_t mqdes1)
{
	printf("Entered Test Send\n");
	struct test *testptr;
	testptr=(struct test *)malloc(sizeof(struct test));
	testptr->a=1;
	testptr->b="Src 1";
	testptr->c="Src 2";
	
	if(mq_send(mqdes1, (const char *)testptr, 128, 1)==-1)
	{
		printf("Queue send failed\n");
	}
	printf("Leaving Test Send\n");
	free(testptr);
}
int main ()
{
	char *qname="/q1";
	mqd_t mqdes;
	mqdes=mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK, 0777, NULL);
	if (mqdes==-1)
	{
		printf("Open queue failed\n");
	}
	printf("Entering Test Send\n");
	testsend(mqdes);
	printf("Returned from Test Send\n");
	printf("Entering Test Receive\n");
	testrecv(mqdes);
	printf("Returned from the Test Receive function and exiting\n");
	mq_close(mqdes);
	mq_unlink(qname);
	return 0;
}

Dumb question - do the character pointers in this structure receive memory references or do you expect them to be created by the message queue in the receive funtion?

struct test
{
	int a;
	char *b;
	char *c;
};
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.