简体   繁体   English

如何读取消息队列中的动态消息

[英]how to read dynamic message in message queue

I need to send a message of not a certain size, and so that the second program displays the size of the received message, allocates memory for it, receives it and prints it我需要发送一条不是特定大小的消息,以便第二个程序显示接收到的消息的大小,为其分配 memory,接收并打印

my first program waits until the user enters a message, allocates memory for it and sends the size of the message to another program and then sends the message itself.我的第一个程序等到用户输入一条消息,为其分配 memory 并将消息的大小发送到另一个程序,然后发送消息本身。

my second program receives a message with the size of the message that will arrive, but at the same time it does not receive the message itself and I do not understand how to solve this problem我的第二个程序收到一条消息,其大小与将要到达的消息一样,但同时它本身没有收到消息,我不明白如何解决这个问题

prog 1. sender编 1. 发件人

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


struct msgbuf
{
long type;
char mtext[1];
};

struct size
{
long type;
char sz[1];
};

  void send_message(int mqid)
 {
char mes[1000];
struct msgbuf *buffer;
struct size buf;
buf.type = 1;
int length;

//memset(buffer.mtext, 0, sizeof(buffer.mtext));

for(int i = 0; i < 4;i++){

    fgets(mes, sizeof(mes), stdin);
    sprintf(buf.sz, "%ld", strlen(mes));
    length = strlen(mes)+1;
    if(msgsnd(mqid, &buf,sizeof(buf.sz), 0)<0)
        perror("msgsnd");
    printf("Size: %d\n",strlen(mes));
    buffer = malloc(sizeof(struct msgbuf)-1 + length);
    buffer->type = 2;
    strncpy(buffer->mtext, mes, strlen(mes));
    if(msgsnd(mqid, &buffer, length, 0)<0)
        perror("msgsnd");
    else
        printf("Send message: %s\n", buffer->mtext);

    //free(buffer);

}
}

 int main()
 {
int key = ftok("comm", 8);
if(key < 0)
    perror("ftok");

int id = msgget(key, 0600);
if(id < 0)
    perror("msgget");

send_message(id);

return 0;
 }

prog 2前 2

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


   struct msgbuf
   {
    long type;
    char mtext[1];
   };

   struct size
   {
    long type;
    char sz[1];
   };



void receive_message(int mqid)
{
    struct msgbuf *buffer;
    struct size szbuf;
    int a;

    for(int i = 0; i < 4;i++){
        if(msgrcv(mqid, &szbuf, sizeof(szbuf.sz), 1,0)<0)
                        perror("msgrcv");
        else
            printf("Size: %s\n", szbuf.sz);

        printf("Hello\n");

        a = atoi(szbuf.sz);
        printf("%d\n", a);

        /*szbuf = (struct size*) malloc(sizeof(struct size) - 1 + a);
        if(msgrcv(mqid, szbuf, sizeof(szbuf->sz), 2,0)<0)
                        perror("msgrcv");
                else
                        printf("Size: %s\n", szbuf->sz);*/

        struct msgbuf* buffer = (struct msgbuf*)malloc(sizeof(struct msgbuf)-1 + a);
        if(msgrcv(mqid, buffer, sizeof(buffer->mtext), 2,0)<0)
            perror("msgrcv");
        else
            printf("Received message: %s\n", buffer->mtext);

        free(buffer);


    }
   }

   int main()
   {
    int key = ftok("comm", 8);
    if(key < 0)
        perror("ftok");

    int id = msgget(key, 0600|IPC_CREAT|IPC_EXCL);
    if(id < 0)
        perror("msgget");

    receive_message(id);

    if(msgctl(id, IPC_RMID, 0)<0)
        perror("msgctl");

    return 0;
   }

the recipient program should print the size of the incoming message and the message itself接收程序应打印传入消息的大小和消息本身

Your case can use the socket ipc, can check msg peak ( also be msg size), then allocate memory and do the real receive.你的情况可以使用socket ipc,可以检查msg peak(也是msg size),然后分配memory并进行真正的接收。

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

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