|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MSGKEY 75
- struct msgform
- {
- long mtype;
- char mtext[512];
-
- };
- int main()
- {
- struct msgform msg;
- int msgqid,pid,*pint;
- msgqid =msgget (MSGKEY,0777);
- pid= getpid ();
- pint =(int*)msg.mtext;
- *pint=pid;
- msg.mtype=1;//定义消息类型
- msgsnd(msgqid,&msg,sizeof(int),0);//发送消息
- msgrcv(msgqid,&msg,512,pid,0);//接受服务者server消息
- printf("client: receive from pid %d\n",*pint);
-
- }
复制代码
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define MSGKEY 75
- struct msgform
- {
- long mtype;
- char mtext[512];
-
- }msg;
- int msgqid;
- int main()
- {
- int i,pid,*pint;
- extern cleanup();
- for(i=0;i<20;i++){//软中断处理
- signal (i,cleanup);
- }
- //建立与顾客进程相同的消息队列
- msgqid=msgget(MSGKEY,0777|IPC_CREAT);
- for(;;)
- {
- msgrcv(msgqid,&msg, 512,1,0);
- pint=(int*)msg.mtext;
- pid =*pint;
- printf ("server:receive from pid %d\n",pid);//接受客户client消息
- msg.mtype= pid;
- *pint= getpid();
- msgsnd(msgqid,&msg,sizeof(int),0);
- }
-
- }
- cleanup()
- {
- msgctl(msgqid,IPC_RMID,0);
- exit (1);
- }
-
-
复制代码 请问为什么在我运行server接收不到消息?
|
-
ipc消息
|