简体   繁体   English

IPC 队列:- 接收错误数据

[英]IPC Queues:- Receiving wrong data

My program is receiving wrong data on msgrcv function if flag is set to 0, however, if i set flag to 1 for specific data then msgrcv function is keep waiting.如果标志设置为 0,我的程序在msgrcv function 上接收错误数据,但是,如果我将特定数据的标志设置为 1,则msgrcv function 将继续等待。 I defined send and receive function in common file and calling it from sender and receiver file.我在公共文件中定义了发送和接收 function 并从发送方和接收方文件中调用它。 I checked debug data and it was sent properly but on receiving end if flag set to 0 means receive first packet then i am getting wrong data.我检查了调试数据,它已正确发送,但在接收端,如果标志设置为 0 意味着接收到第一个数据包,那么我收到了错误的数据。 If set flag to 1 which means specific message type data msgrcv function ended in waiting state.如果将标志设置为 1,则表示特定消息类型数据msgrcv function 以等待 state 结束。 Please help.请帮忙。 / CommonMsgQueue.c / / CommonMsgQueue.c /

    int InitializeMsgQueue(char *keypath)
    {
       key_t key;
       int msgid;
       key = ftok(keypath,'B');
       if (key == -1 )
       {
          perror("Failed to generate key ");
          exit(0);
       }
       msgid = msgget(key, 0666 | IPC_CREAT );
       if ( msgid == -1 )
       {
          perror("Failed to get msgid");
          exit(0);
       }
       return msgid;
    }
    int SendMsg(key_t msgqid ,StMesgQ *MsgData , int msglength)
    {
        int iRetVal =0;
        iRetVal = msgsnd(msgqid,&MsgData,sizeof(StMesgQ),IPC_NOWAIT);
        if ( iRetVal == -1 )
        {
           perror("Failed to send message");
        }
        return iRetVal;
    }
    int RcvMsg(key_t msgqid, StMesgQ *MsgData , int msglength)
    {
       int iRetVal =0;
       iRetVal = msgrcv(msgqid,&MsgData,msglength,0,0);
       if (iRetVal == -1 )
       {
          perror("Failed to receive message ");
          exit(0);
       }
       
       return iRetVal;
    
    }
   /*Sender.c*/
   msgqid = InitializeMsgQueue("/home/nprabhat/Nitesh/Nitesh_FrameWork/Key/ClientQueue");
   CommonSHMData = (struct CommonSHM*)ShmData;
   StMesgQ obj;
   while (1)
   {
       
       printf("Sender:-- Enter the value For iIndex\n");
       scanf("%d",&value);
       CommonSHMData->iIndex = value;
       CommonSHMData->iIndex2 = value;
       DataPktToSend = (char*)CommonSHMData;
     memcpy(&obj.buf,&DataPktToSend,strlen(DataPktToSend));
       obj.iMsgType = 1;
       SendMsg(msgqid,&obj,sizeof(StMesgQ));
    }
  /*Reciever.c*/
  RcvMsg(msgqid,&MsgData,sizeof(StMesgQ));
  CommonSHMData_Recv = (struct CommonSHM*)MsgData.buf;
  printf("Data Received from queue is %d",CommonSHMData_Recv->iIndex);

regarding:关于:

   iRetVal = msgrcv( msgqid, &MsgData, msglength, 0, 0 );
   
   if (iRetVal == -1 )
   {
      perror("Failed to receive message ");
      exit(0);
   }
   
   memcpy(&MsgData,&RcvMsgData,sizeof(StMesgQ));

the function: memcpy() copies from right to left. function: memcpy()从右到左复制。

Here is the syntax:这是语法:

void *memcpy(void *destination, const void *source, size_t n);

So the call to memcpy() is overlaying the data placed in MsgData by the call to msgrcv() with whatever trash happens to be in RcvMsgData因此,对memcpy()的调用将通过对msgrcv()的调用将放置在MsgData中的数据与 RcvMsgData 中发生的任何垃圾RcvMsgData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM