- 论坛徽章:
- 0
|
程序如下:主要问题出在 msgrcv 的msgsnd 上中使用MSG_REC型数据,
如何用MSG_REC结构在消息队列中接收,发送消息
#include <stdio.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/ipc.h>;
#include <sys/msg.h>;
typedef struct msg_rec
{
long mtype;
char mtext[2098];
}MSG_REC;
int main()
{
char *str;
int pid,msgid;
char str2[255];
MSG_REC aa, bb;
str=(char *)malloc(255);
msgid=msgget(0x00002000,IPC_CREAT|0660);
if(msgid<0)
{
printf("message initial false!!\n" ;
return -1;
}
memset(str,0,255);
sprintf(aa.mtext,"the string transported by message" ;
sprintf(aa.mtype,"%d",112233);
printf("str1:[%s] type=[%d]\n",aa.mtext,aa.mtype);
msgsnd(msgid,&aa,255,0);
printf("send ok\n" ;
memset(bb.mtext,0,255);
msgrcv(msgid,&bb,255,112233,0);
printf("str1:[%s] type=[%d]\n",bb.mtext,bb.mtype);
return 0; |
|