的广播节目this.I使用了3分不同的密钥,但只有第一个监听获取其他message.the我们得到消息发送和error.The键应该识别不同的用户应该不会吧?消息队列mutilple接收机实现
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
struct msgbuf
{
long mtype;
char mtext[140];
}send_buf,recv_buf;
int send_msg_id,send_msg_id1,send_msg_id2;
key_t send_key,send_key1,send_key2;
int main()
{
send_key=ftok("broadcast.c",'A');
send_key1=ftok("broadcast.c",'B');
send_key2=ftok("broadcast.c",'C');
char save[140];
if (send_key==-1)
{
perror("\nCaller send key error(ftok)");
exit(1);
}
send_msg_id=msgget(send_key,0666 | IPC_CREAT);
send_msg_id1=msgget(send_key,0555 | IPC_CREAT);
send_msg_id2=msgget(send_key,0777 | IPC_CREAT);
if (send_msg_id==-1)
{
printf("\nCaller send msgget error");
exit(1);
}
printf("\nCALLER:");
while (fgets(send_buf.mtext,sizeof(send_buf.mtext),stdin)!=NULL)
{
send_buf.mtype=1;
int len=strlen(send_buf.mtext);
strcpy(save,send_buf.mtext);
if (send_buf.mtext[len-1]=='\n');
send_buf.mtext[len-1]='\0';
if (msgsnd(send_msg_id,&send_buf,len+1,0)==-1)
printf("\nMsg sending error\n");
strcpy(send_buf.mtext,save);
if (msgsnd(send_msg_id1,&send_buf,len+1,0)==-1)
perror("\nMsg sending error (send_msg_id1)");
strcpy(send_buf.mtext,save);
if (msgsnd(send_msg_id2,&send_buf,len+1,0)==-1)
perror("\nMsg sending error (send_msg_id2)");
}
int i=0;
while(i<9999)
i++;
msgctl(send_msg_id,IPC_RMID,NULL);
return 0;
}