Linux下的消息队列MsgQueue

server文件运行时先收消息再发消息,但是server.c中创建了消息队列,所以先运行server

client文件运行时先发消息再收消息

运行结果:打开两个terminal分别进行

Linux下的消息队列MsgQueue_第1张图片

comm.h头文件

  1 #ifndef _COMM_H_
  2 #define _COMM_H_
  3 
  4 
  5 #include
  6 #include
  7 #include
  8 #include
  9 #include
 10 
 11 #define SERVER 1
 12 #define CLIENT 2
 13 
 14 #define PATHNAME "."
 15 #define PROJ_ID 0x6666
 16 
 17 struct msgbuf
 18 {
 19     long mtype;
 20     char mtext[1024];
 21 };
 22 
 23 int creat();      //创建消息队列
 24 int send(int msgid,int who,char*msg);    //发送消息
 25 int recv(int msgid,int type,char out[]); //接收消息
 26 int destory(int msgid);       //销毁消息队列
 27 int get();       //获得消息队列的标识符
 28 #endif


comm.c

  1 #include"comm.h"
  2 
  3 static int com(int flags)
  4 {
  5     key_t key=ftok(PATHNAME,PROJ_ID);
  6     if(key<0)
  7     {
  8         perror("ftok");
  9         return -1;
 10     }
 11     int msgid=msgget(key,flags);
 12     if(msgid<0)
 13     {
 14         perror("msgget");
 15         return -2;
 16     }
 17     return msgid;
 18 }
 19 
 20 int creat()
 21 {
 22     return com(IPC_CREAT|IPC_EXCL|0666);
 23 }
 24 
 25 int destory(int msgid)
 26 {
 27     if(msgctl(msgid,IPC_RMID,NULL)<0)
 28     {
 29         perror("msgctr");
 30         return -1;
 31     }
 32 }
 33 
 34 int get()
 35 {
 36     return com(IPC_CREAT);
 37 }
 38 
 39 int send(int msgid,int who,char *msg)
 40 {
 41     struct msgbuf buf;
 42     buf.mtype=who;
 43     strcpy(buf.mtext,msg);
 44     if(msgsnd(msgid,(void*)&buf,sizeof(buf.mtext),0)<0)
 45     {
 46         perror("msgsnd");
 47         return -1;
 48     }
 49 }
 50 
 51 int recv(int msgid,int type,char out[])
 52 {
 53     struct msgbuf buf;
 54     if(msgrcv(msgid,(void*)&buf,sizeof(buf.mtext),type,0)<0)
 55     {
 56         perror("msgrcv");
 57         return -1;
 58     }
 59     strcpy(out,buf.mtext);
 60     return 0;
 61 }
 62 


server.c

  1 #include"comm.h"
  2 
  3 int main()
  4 {
  5     int  msgid=creat();
  6     char buf[1024];
  7     while(1)
  8     {
  9         buf[0]=0;
 10         recv(msgid,CLIENT,buf);
 11         printf("clien say #%s\n",buf);
 12 
 13         printf("please enter#");
 14         fflush(stdout);
 15         ssize_t s=read(0,buf,sizeof(buf)-1);
 16         if(s>0)
 17         {
 18             buf[s]=0;
 19             send(msgid,SERVER,buf);
 20         }
 21     }
 22     destory(msgid);
 23     return 0;
 24 }

client.c

  1 #include"comm.h"
  2 
  3 int main()
  4 {
  5     int msgid=get();
  6     char buf[1024];
  7     while(1)
  8     {
  9         buf[0]=0;
 10         printf("please enter#");
 11         fflush(stdout);
 12         ssize_t s=read(0,buf,sizeof(buf)-1);
 13         if(s>0)
 14         {
 15             buf[s]=0;
 16             send(msgid,CLIENT,buf);
 17         }
 18 
 19         recv(msgid,SERVER,buf);
 20         printf("server say#%s\n",buf);
 21     }
 22     return 0;
 23 }
 24 


Makefile.c

  1 .PHONY:all
  2 all:server client
  3 server:server.c comm.c
  4     gcc -o $@ $^
  5 client:client.c comm.c
  6     gcc -o $@ $^
  7 .PHONY:clean
  8 clean:
  9     rm -f server cli




你可能感兴趣的:(Linux)