C 如何读取消息队列中的动态消息
我需要发送一条不一定大小的消息,这样第二个程序就会显示接收到的消息的大小,为它分配内存,接收并打印它 我的第一个程序等待用户输入消息,为它分配内存,并将消息大小发送给另一个程序,然后发送消息本身 我的第二个程序接收到一条消息,该消息的大小与将要到达的消息的大小相同,但同时它没有接收到消息本身,我不知道如何解决这个问题 项目1。寄件人C 如何读取消息队列中的动态消息,c,linux,ubuntu,message-queue,C,Linux,Ubuntu,Message Queue,我需要发送一条不一定大小的消息,这样第二个程序就会显示接收到的消息的大小,为它分配内存,接收并打印它 我的第一个程序等待用户输入消息,为它分配内存,并将消息大小发送给另一个程序,然后发送消息本身 我的第二个程序接收到一条消息,该消息的大小与将要到达的消息的大小相同,但同时它没有接收到消息本身,我不知道如何解决这个问题 项目1。寄件人 #include <stdio.h> #include <string.h> #include <sys/ipc.h> #inc
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构msgbuf
{
长型;
字符多行文字[1];
};
结构大小
{
长型;
charsz[1];
};
无效发送_消息(int mqid)
{
char-mes[1000];
结构msgbuf*缓冲区;
结构尺寸buf;
buf.type=1;
整数长度;
//memset(buffer.mtext,0,sizeof(buffer.mtext));
对于(int i=0;i<4;i++){
fgets(mes、mes、stdin);
sprintf(buf.sz,“%ld”,strlen(mes));
长度=strlen(mes)+1;
if(msgsnd(mqid,&buf,sizeof(buf.sz),0)type=2;
strncpy(缓冲区->多行文字、人工编码站、strlen(人工编码站));
if(msgsnd(mqid,&buffer,length,0)多行文字);
//自由(缓冲);
}
}
int main()
{
int键=ftok(“通信”,8);
如果(键<0)
佩罗尔(“ftok”);
int id=msgget(编号0600);
if(id<0)
佩罗尔(“msgget”);
发送消息(id);
返回0;
}
程序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;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构msgbuf
{
长型;
字符多行文字[1];
};
结构大小
{
长型;
charsz[1];
};
无效接收_消息(int mqid)
{
结构msgbuf*缓冲区;
结构尺寸szbuf;
INTA;
对于(int i=0;i<4;i++){
if(msgrcv(mqid,&szbuf,sizeof(szbuf.sz),1,0)sz),2,0)sz)*/
struct msgbuf*buffer=(struct msgbuf*)malloc(sizeof(struct msgbuf)-1+a);
if(msgrcv(mqid,buffer,sizeof(buffer->mtext),2,0)mtext);
自由(缓冲);
}
}
int main()
{
int键=ftok(“通信”,8);
如果(键<0)
佩罗尔(“ftok”);
int id=msgget(键,0600,IPC|U创建,IPC|U不包括);
if(id<0)
佩罗尔(“msgget”);
接收消息(id);
如果(msgctl(id,IPC\u RMID,0)您的案例可以使用套接字IPC,可以检查消息峰值(也可以是消息大小),然后分配内存并执行真正的接收。我找不到您的代码有任何错误。有一条建议,您不必使用msgsnd手册页中提到的原型,即msgrcv。您可以声明任何大小的多行文字,并防止一些复杂的malloc计算