首页 文章

为什么我无法使用msgrcv()访问消息队列中的消息文本?

提问于
浏览
0

我已经构建了一个程序,允许我创建和删除消息队列以及发送和接收消息 .

除接收消息外,一切似乎都正常工作 . 当我收到结构时,我可以访问该类型(我一直用来表示“收件人”)并打印它,但是存储在结构的msg字段中的字符串将不会打印 . 在使用msgrcv()之后,printf()似乎成功访问了mbuf.type字段,但没有访问mbuf.msg . 我已经尝试过调试以找出问题所在,但到目前为止我还没有成功 .

消息似乎被发送到队列,因为当我使用“ipcs -q”查看我的内核的消息队列时,它将正确显示我发送的消息数 . 我还能够在我发送消息的程序实例中访问和打印msg字段 . 直到程序退出并且我使用“-r”标志重新启动它,我才能打印msg字段 .

我已经包含了下面的代码,包括包含我的消息struct定义的头文件 .

请注意:我知道我的验证很笨重,我计划在程序正常工作后简化它 . 如果它令人困惑,我道歉 .

标头文件:

#ifndef MESSAGE_BUFFER
#define MESSAGE_BUFFER

#define MSG_MAX 4056

typedef struct messageBuffer
{
    long recipient; //recipient of message
    long senderID; //id number of the sender    
    char msg[MSG_MAX]; //content of the message
}messageBuffer;

#endif

主要:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include "message.h"


//function to print usage information upon error
void printUsage(){
    printf("Error: Invalid arguments\n"); 
    printf("Usage:\n");
    printf("(Create Queue) <-c/C> <key>\n");
    printf("(Send a message) <-s/S> <key> <recipient_id> <message>\n"); 
    printf("(Receive a message) <-r/R> <key> <recipient_id>\n");        printf("(Delete queue) <-d/D> <key>\n");
}


//main
int main(int argc, char **argv){

//declare necessary variables
char flag;
int msqid;
messageBuffer mbuf;
size_t buf_length;
int msgflg = IPC_CREAT | 0666;
key_t key;
unsigned long recipient;



//validate arguments
if((argc < 3 || argc > 5) || argv[1][0] != '-' || strlen(argv[1]) != 2){    
    printf("%d\n", argc);   
    printUsage();
    return -1;  
}

flag = argv[1][1];


switch(flag){

        //Create a message queue
    case 'c':
    case 'C':
        if((key = atoi(argv[2])) < 1){
            printf("Error assigning key");
            return -1;
        }
        if((msqid = msgget(key, msgflg)) < 1){
            printf("Error creating queue:");
            return -1;
        }
        printf("%s%i\n", "Key: ", key);
        printf("%s%i\n", "msqid: ", msqid);
        break;

        //Send a message
    case 's':
    case 'S':
        if((key = atoi(argv[2])) < 1){
            perror("Error assigning key:");
            return -1;
        }
        if((msqid = msgget(key, 0400)) < 1){
            perror("Error accessing queue:");
        }
        mbuf.recipient = atoi(argv[3]);
        strncpy(mbuf.msg, argv[4], MSG_MAX);
        buf_length = strlen(mbuf.msg) + 1;
        if(msgsnd(msqid, &mbuf, buf_length, 0) < 0){
            perror("Error sending message:");
            return -1;
        }   
        printf("Message sent (%lu): %s\n", mbuf.recipient, mbuf.msg);

        break;

        //Receive a message
    case 'r':
    case 'R':
        if((key = atoi(argv[2])) < 1){
            perror("Error assigning key:");
            return -1;
        }
        if((msqid = msgget(key, 0400)) < 1){
            perror("Error accessing queue:");
        }
        recipient = atoi(argv[3]);
        if( msgrcv(msqid, &mbuf, MSG_MAX, recipient, 0)< 0){
            perror("Error receiving message:");
            return -1;
        }
        printf("Message received (%lu):\n", mbuf.recipient);
        printf("%s\n", mbuf.msg);
        break;

        //Delete a message queue
    case 'd':
    case 'D':
        if((key = atoi(argv[2])) < 1){
            perror("Error assigning key");
            return -1;
        }
        if((msqid = msgget(key, 0400)) < 1){
            perror("Error accessing queue:");
        }


        printf("%d\n", msqid);
        if((msgctl(msqid, IPC_RMID, NULL)) < 0){
            perror("Delete message queue failed:");
        }
        break;  

        //invalid flag
    default:
        printUsage();
        return -1;      
}






return 0;
}

我很感激任何意见 . 谢谢 .

1 回答

  • 0

    事实证明,问题在于指定传递的结构的大小 . 我更改了代码“buf_length = strlen(mbuf.msg)1;” to buf_length =(sizeof(mbuf.msg)1);

相关问题