linux 消息队列

来源:互联网 发布:菩提迦耶 知乎 编辑:程序博客网 时间:2024/06/05 21:09

主要使用函数清单:

// 键值构建函数#include <sys/types.h>#include <sys/ipc.h>key_t ftok(const char *pathname, int proj_id);// 创建与获取消息队列函数#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>int msgget(key_t, int msgflg);// 发送消息函数#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);// 接收消息函数#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);// 消息控制函数#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>int msgctl(int msqid, int cmd, struct msqid_ds *buf);


服务端:

#include <stdio.h>  #include <string.h>  #include <stdlib.h>  #include <errno.h>  #include <unistd.h>  #include <sys/types.h>  #include <sys/ipc.h>  #include <sys/stat.h>  #include <sys/msg.h>  #define MSG_FILE "server.c"  #define BUFFER 255  /*S_IRUSR  Permits the file's owner to read it.S_IWUSR  Permits the file's owner to write to it.S_IRGRP  Permits the file's group to read it.S_IWGRP  Permits the file's group to write to it.*/#define PERM S_IRUSR|S_IWUSR   struct msgtype {  long mtype;  char buffer[BUFFER+1];  };  int main()  {  struct msgtype msg;  key_t key;  int msgid;   //  创建键值if((key=ftok(MSG_FILE,'a'))==-1)  {  fprintf(stderr,"Creat Key Error:%s\a\n",strerror(errno));  exit(1);  }  // 创建消息队列if((msgid=msgget(key,PERM|IPC_CREAT|IPC_EXCL))==-1)  {  fprintf(stderr,"Creat Message Error:%s\a\n",strerror(errno));  exit(1);  }  while(1)  {  // 从队列中获取消息msgrcv(msgid,&msg,sizeof(struct msgtype),1,0);  fprintf(stderr,"Server Receive:%s\n",msg.buffer);  msg.mtype=2;  // 发送消息msgsnd(msgid,&msg,sizeof(struct msgtype),0);  }  exit(0);  }

客户端:

#include <stdio.h>  #include <string.h>  #include <stdlib.h>  #include <errno.h>  #include <sys/types.h>  #include <sys/ipc.h>  #include <sys/msg.h>  #include <sys/stat.h>  #define MSG_FILE "server.c"  #define BUFFER 255  #define PERM S_IRUSR|S_IWUSR  struct msgtype {  long mtype;  char buffer[BUFFER+1];  };  int main(int argc,char **argv)  {  struct msgtype msg;  key_t key;  int msgid;  if(argc!=2)  {  fprintf(stderr,"Usage:%s string\n\a",argv[0]);  exit(1);  }  if((key=ftok(MSG_FILE,'a'))==-1)  {  fprintf(stderr,"Creat Key Error:%s\a\n",strerror(errno));  exit(1);  }  if((msgid=msgget(key,PERM))==-1)  {  fprintf(stderr,"Creat Message Error:%s\a\n",strerror(errno));  exit(1);  }  msg.mtype=1;  strncpy(msg.buffer,argv[1],BUFFER);  msgsnd(msgid,&msg,sizeof(struct msgtype),0);  memset(&msg,'\0',sizeof(struct msgtype));  msgrcv(msgid,&msg,sizeof(struct msgtype),2,0);  fprintf(stderr,"Client receive:%s\n",msg.buffer);  exit(0);  }


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩长高长的慢怎么办 小孩长高长得慢怎么办 小孩吃东西不吸收营养怎么办 婴儿长得太快怎么办 2个月婴儿长太快怎么办 孩子脚长得太快怎么办 4个月宝宝缺钙怎么办 2个月宝宝不长肉怎么办 小孩子长得不高怎么办 宝宝误吃蜂蜜了怎么办 有人拿着吃完的东西退货怎么办 婴儿个子长的慢怎么办 儿童长得太快怎么办 宝宝个子长太快怎么办 孩子九个月奶水不够怎么办 小孩起热痱子痒怎么办 媳妇生完小孩奶水出不来怎么办 生完孩子下奶疼怎么办 生完小孩没奶水怎么办 孩子半个月奶水越来越少怎么办 半个月后奶水越来越少怎么办 坐月子半个月奶水越来越少怎么办 孩子七个月奶水越来越少怎么办 生完孩子奶水越来越少怎么办 生完孩子回奶了怎么办 产妇3天没奶水怎么办 产后7天了奶水少怎么办 刚生完小孩没有奶水怎么办 突然就没奶水了怎么办 生完小孩没有奶水怎么办 生完宝宝没有奶怎么办 剖腹产奶涨的疼怎么办 生产一天了没奶怎么办 第一天断奶 奶水一直流出怎么办 新生儿刚出生没奶水怎么办 安卓手机死机了怎么办 婴幼儿几天不拉大便怎么办 樱桃吃多了胃不舒服怎么办 空腹吃水果胃不舒服怎么办 吃水果后胃不舒服怎么办 吃水果伤胃了怎么办