POSIX message queue error in mq_receive()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 16
Reputation: katwalatapan is an unknown quantity at this point 
Solved Threads: 0
katwalatapan katwalatapan is offline Offline
Newbie Poster

POSIX message queue error in mq_receive()

 
0
  #1
Oct 30th, 2009
Hello,

Could anyone please explain a reason why data could not be received from a POSIX message queue using mq_receive(), called from a different file. Following is my code for a test that i'm running to try and transfer data from one file to another.

main.c

  1. #include "recfun.h"
  2.  
  3.  
  4. int main()
  5. {
  6.  
  7. datastruct *data;
  8. char *qname="/queue";
  9. qdes=mq_open(qname, O_RDWR|O_CREAT|O_NONBLOCK, 0777, NULL);
  10. if(qdes==-1)
  11. {
  12. printf("Cannot open queue\n");
  13. }
  14. data=(datastruct*)malloc(sizeof(datastruct));
  15. data->i=3;
  16. data->c="3 is the data";
  17. printf("data->i=%d\n", data->i);
  18. printf("data->c=%s\n", data->c);
  19. if(mq_send(qdes, (char *)data, 128, 0)==-1)
  20. {
  21. printf("Cannot send data onto the queue\n");
  22. }
  23. recv_fun(qdes);
  24. free(data);
  25. mq_close(qdes);
  26. mq_unlink(qname);
  27.  
  28. return (0);
  29. }

recfun.h

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mqueue.h>
  4. #include <string.h>
  5.  
  6. typedef struct
  7. {
  8. int i;
  9. char *c;
  10. }datastruct;
  11. static mqd_t qdes;
  12. void recv_fun(mqd_t qdes);

recfun.c
  1. #include "recfun.h"
  2.  
  3. void recv_fun(mqd_t qdes)
  4. {
  5. datastruct *data1;
  6. data1=(datastruct *)malloc(sizeof(datastruct));
  7. if(mq_receive(qdes, (char *)&data1, 128, 0)==-1)
  8. {
  9. printf("Cannot receive data from queue\n");
  10. }
  11. printf("data1->i=%d\n", data1->i);
  12. printf("data1->c=%s\n", data1->c);
  13. free(data1);
  14. }

Please do let me know if there could be any reason why I receive "Message too long" error from mq_receive, even when I have allocated sufficient memory.

Thank you.

Regards,

Tapan.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC