UNIX消息队列msgrcv未能收到消息 [英] UNIX message queue msgrcv failed to receive message

查看:411
本文介绍了UNIX消息队列msgrcv未能收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,
任何想法,为什么msgrcv接收空白缓冲区?

下面是code:

 在此输入code
 #包括LT&; SYS / msg.h>
 #包括LT&;&unistd.h中GT;
 #包括LT&; SYS / types.h中>
 #包括LT&;&stdio.h中GT;
 #包括LT&;&string.h中GT; typedef结构mymsg {
  长MTYPE;
  多行文字的cha​​r [24];
 } mymsg; 诠释的main()
 {
  将对msqid INT;
  mymsg味精,浅黄色;
  =将对msqid了msgget(IPC_PRIVATE,IPC_CREAT | IPC_EXCL);  如果(将对msqid == - 1){
  PERROR(无法创建消息队列\\ n);
  }
  其他{
  的printf(消息队列ID:%U \\ N,将对msqid);
  }
  msg.mtype = 1;
  的strcpy(msg.mtext,这是一个信息);
  如果(的msgsnd(将对msqid,&放大器;味精,sizeof的(msg.mtext),0)== - 1){
   PERROR(的msgsnd失败:);
  }
  其他{
   的printf(消息发送成功的\\ n);
  }
 //为ssize_t msgrcv(INT将对msqid的,无效* msgp,为size_t msgsz,长msgtyp,INT msgflg);  // msgrcv(将对msqid,buff.mtext,sizeof的(msg.mtext),1,0);这是错误
  msgrcv(将对msqid,&安培; BUFF,sizeof的(msg.mtext),1,0); //这是正确的(感谢埃里克)
  的printf(收到的消息是:%S \\ n,buff.mtext);
 }   输出:
   [根@ dhcppc0 message_queue]#./a.out
   消息队列ID:294919
   消息发送成功
   收到的消息是:
                                                    1,1顶


解决方案

msgbuf.mtype 必须设置为1 - 因为你告诉 msgrcv 要1型的消息。

另外,你可以 msgbuf.mtype 设置为任何正值,然后告诉 msgrcv 您需要的任何通过传递0作为 msgtyp 参数信息类型。

此外, msgrcv 预期的指针, msgbuf

  msgrcv(将对msqid,&安培; BUFF,sizeof的(msg.mtext),1,0);

编辑:测试工作源:

 的#include< SYS / msg.h>
 #包括LT&;&unistd.h中GT;
 #包括LT&; SYS / types.h中>
 #包括LT&;&stdio.h中GT;
 #包括LT&;&string.h中GT; typedef结构mymsg {
  长MTYPE;
  多行文字的cha​​r [24];
 } mymsg; 诠释的main()
 {
  将对msqid INT;
  mymsg味精,浅黄色;
  =将对msqid了msgget(IPC_PRIVATE,IPC_CREAT | IPC_EXCL);  如果(将对msqid == - 1){
  PERROR(无法创建消息队列\\ n);
  }
  其他{
  的printf(消息队列ID:%U \\ N,将对msqid);
  }
  msg.mtype = 1; //在那里无法复制
  的strcpy(msg.mtext,这是一个信息);
  如果(的msgsnd(将对msqid,&放大器;味精,sizeof的(msg.mtext),0)== - 1){
   PERROR(的msgsnd失败:);
  }
  其他{
   的printf(消息发送成功的\\ n);
  }
 //为ssize_t msgrcv(INT将对msqid的,无效* msgp,为size_t msgsz,长msgtyp,INT msgflg);  msgrcv(将对msqid,&安培; BUFF,sizeof的(msg.mtext),1,0);
  的printf(收到的消息是:%S \\ n,buff.mtext);
 }

Dear Friends, Any idea why the msgrcv is receiving a blank buffer?

Here is the code:

enter code here
 #include <sys/msg.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>

 typedef struct mymsg {
  long mtype;
  char mtext[24];
 }mymsg;

 int main()
 {
  int msqid;
  mymsg msg,buff;
  msqid=msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL);

  if(msqid==-1){
  perror("FAiled to create message queue\n");
  }
  else{
  printf("Message queue id:%u\n",msqid);
  }
  msg.mtype=1;
  strcpy(msg.mtext,"This is a message");
  if(msgsnd(msqid,&msg,sizeof(msg.mtext),0)==-1){
   perror("msgsnd failed:");
  }
  else{
   printf("Message sent successfully\n");
  }
 //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);

  // msgrcv(msqid,buff.mtext,sizeof(msg.mtext),1,0); This was error
  msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);  // This is correct (Thanks to Erik)
  printf("The message received is: %s\n",buff.mtext);
 }

   Output:
   [root@dhcppc0 message_queue]# ./a.out
   Message queue id:294919
   Message sent successfully
   The message received is: 
                                                    1,1           Top

解决方案

msgbuf.mtype must be set to 1 - since you're telling msgrcv that you want messages of type 1.

Alternatively, you could set msgbuf.mtype to any positive value, and then tell msgrcv that you want any message type by passing 0 as the msgtyp argument.

Also, msgrcv expects a pointer to a msgbuf:

msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);

EDIT: Tested working source:

 #include <sys/msg.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>

 typedef struct mymsg {
  long mtype;
  char mtext[24];
 }mymsg;

 int main()
 {
  int msqid;
  mymsg msg,buff;
  msqid=msgget(IPC_PRIVATE,IPC_CREAT|IPC_EXCL);

  if(msqid==-1){
  perror("FAiled to create message queue\n");
  }
  else{
  printf("Message queue id:%u\n",msqid);
  }
  msg.mtype=1; // was there failed to copy
  strcpy(msg.mtext,"This is a message");
  if(msgsnd(msqid,&msg,sizeof(msg.mtext),0)==-1){
   perror("msgsnd failed:");
  }
  else{
   printf("Message sent successfully\n");
  }
 //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);

  msgrcv(msqid,&buff,sizeof(msg.mtext),1,0);
  printf("The message received is: %s\n",buff.mtext);
 }

这篇关于UNIX消息队列msgrcv未能收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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