#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<fcntl.h>#include<errno.h>#include<sys/msg.h>#include<mqueue.h>#defineBUF_SIZE1024#defineFILE_PATH_S"/mq_sw"#defineFILE_PATH_C"/mq_cw"intmain(){int ret=0;int len=0;unsignedint prio=0;char*buf=NULL;long mq_msgsize=0;// forkpid_t pid=fork();if(-1==pid){perror("fork");exit(-1);}if(0==pid){// clientmqd_t mq_id_cw=0;mqd_t mq_id_cr=0;// open mq
mq_id_cw=mq_open(FILE_PATH_C,O_WRONLY | O_CREAT,0666,NULL);if(-1==mq_id_cw){perror("mq_open");exit(-1);}
mq_id_cr=mq_open(FILE_PATH_S,O_RDONLY);if(-1==mq_id_cr){perror("mq_open");exit(-1);}// get mq attrstructmq_attr mq_attr_cr;structmq_attr mq_attr_cw;
ret=mq_getattr(mq_id_cr,&mq_attr_cr);if(-1==ret){perror("mq_getattr");exit(-1);}
ret=mq_getattr(mq_id_cw,&mq_attr_cw);if(-1==ret){perror("mq_getattr");exit(-1);}// display mq attrprintf("mq_attr:mq_msgsize=%ld\n",mq_attr_cr.mq_msgsize);printf("mq_attr:mq_maxmsg=%ld\n",mq_attr_cr.mq_maxmsg);printf("mq_attr:mq_flags=%ld\n",mq_attr_cr.mq_flags);printf("mq_attr:mq_curmsgs=%ld\n",mq_attr_cr.mq_curmsgs);// save mq_msgsize
mq_msgsize=mq_attr_cr.mq_msgsize;// init buf
buf=(char*)malloc(mq_msgsize);if(NULL==buf){printf("malloc: Failed to allocate memory!");exit(-1);}for(;;){printf("enter 'exit' to close!\n>");bzero(buf,mq_msgsize);scanf("%s",buf);//send to server
len=mq_send(mq_id_cw,buf,strlen(buf),0);if(-1==len){printf("send message failed!\n");perror("write");}else{printf("send successful!\n");}if(0==strcmp(buf,"exit")){// close message queuemq_close(mq_id_cw);mq_close(mq_id_cr);mq_unlink(FILE_PATH_C);printf("client closed!\n");break;}//read from serverbzero(buf,mq_msgsize);
len=mq_receive(mq_id_cr,buf,mq_msgsize,&prio);if(-1==len){perror("mq_receive");}else{printf("recv message from server!\ndata:%s\n",buf);}}free(buf);}else{//serverint fd=0;mqd_t mq_id_sr=0;mqd_t mq_id_sw=0;// open mq
mq_id_sw=mq_open(FILE_PATH_S,O_WRONLY | O_CREAT,0666,NULL);if(-1==mq_id_sw){perror("mq_open");exit(-1);}
mq_id_sr=mq_open(FILE_PATH_C,O_RDONLY);if(-1==mq_id_sr){perror("mq_open");exit(-1);}// get mq attrstructmq_attr mq_attr_sr;structmq_attr mq_attr_sw;
ret=mq_getattr(mq_id_sr,&mq_attr_sr);if(-1==ret){perror("mq_getattr");exit(-1);}
ret=mq_getattr(mq_id_sw,&mq_attr_sw);if(-1==ret){perror("mq_getattr");exit(-1);}// don't display; save some info
mq_msgsize=mq_attr_sr.mq_msgsize;// init buf
buf=(char*)malloc(mq_msgsize);if(NULL==buf){printf("malloc: Failed to allocate memory!");exit(-1);}for(;;){bzero(buf,mq_msgsize);// read from client
len=mq_receive(mq_id_sr,buf,mq_msgsize,&prio);if(-1==len){perror("mq_receive");}elseif(1>=strlen(buf)){// nothing to do}else{// if recv 'exit' to closeif(0==strcmp(buf,"exit")){mq_close(mq_id_sw);mq_close(mq_id_sr);mq_unlink(FILE_PATH_S);printf("[SERVER]:closed!\n");break;}// open file and read
fd=open(buf,O_RDONLY);if(-1==fd){char*error_msg=strerror(errno);bzero(buf,mq_msgsize);strcpy(buf,error_msg);}else{bzero(buf,mq_msgsize);read(fd,buf,mq_msgsize);}// send message to clientmq_send(mq_id_sw,buf,strlen(buf),0);close(fd);}}free(buf);}}