1.消息结构体
struct msgbuf { long mtype;/* message type, must be > 0 */ data_t data;/* message data */ };
data_t data; 是自己定义的
2.进程间消息队列发送消息函数msgsnd
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 发消息时的msgtype用法:
指定发送的消息类型 ,消息类型大于0
3.进程间消息队列接收消息函数msgsnd #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);
接收消息时的msgtype的用法:
(1)msgtype > 0
接收消息类型为msgtype的消息
(2)msgtype==0
接收消息队列中最前面的那个消息
下面举个例子,客户端发送消息类型为 1 、2 、3共三种类型,然后服务端指定接收类型2
客户端代码
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <errno.h> #include <unistd.h> extern int errno; #define ClientKey 1234 typedef struct stu_msgbuf_s { long msgtype; char msgtext[1024]; }stu_msgbuf_t; int sendmsg(int msgid ,const char * pchBuf, int len,int msgtype) { stu_msgbuf_t stuSendMsg = {0}; int ret = -1; if(NULL == pchBuf || strlen(pchBuf) == 0 || len <=0) { printf("\nparam error [sendmsg]\n"); return -1; } stuSendMsg.msgtype = msgtype; memset(stuSendMsg.msgtext,0,sizeof(stuSendMsg.msgtext)); strncpy(stuSendMsg.msgtext,pchBuf,sizeof(stuSendMsg.msgtext)-1); ret = msgsnd(msgid,&stuSendMsg,sizeof(stuSendMsg.msgtext),IPC_NOWAIT); if(-1 == ret) { printf("\n msgsnd gs_nClientMsgid [%d] errno=%d [%s]\n",msgid,errno,strerror(errno)); return -1; } else { printf("\nmsg snd [%s]\n",stuSendMsg.msgtext); } return 0; } int init(int *pMsgId) { int msgid = -1; key_t ktmp = ftok("/share/1.tmp",ClientKey); if(ktmp == -1) { printf("\n if(ktmp == -1) errno=%d [%s]\n",errno,strerror(errno)); return -1; } else { printf("\n ftok ktmp[%d]\n",ktmp); } msgid = msgget(ktmp, 0666|IPC_CREAT); if(msgid < 0) { printf("\n if(gs_nClientMsgid == -1) errno=%d [%s]\n",errno,strerror(errno)); return -1; } printf("\n msgget sucess %d ktmp[%d]\n",msgid,ktmp); *pMsgId = msgid; return 0; } int main() { int ret =-1; int msgid =-1; ret = init(&msgid); if(ret == -1) { return 0; } while(1) { char chStr[20] = {0}; int type = 0; static int i = 0; type = i%3+1; i++; printf("\ntype[%d]\n",type); snprintf(chStr,sizeof(chStr),"type[%d]--[%ld]",type,time(NULL)); sendmsg(msgid,chStr, strlen(chStr)+1,type); sleep(2); } }
服务端代码
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <errno.h> #include <unistd.h> extern int errno; #define lientKey 1234 static int gs_nClientMsgid = -1; typedef struct stu_msgbuf_s { long msgtype; char msgtext[1024]; }stu_msgbuf_t; int rcvmsg(int msgid,const char * pchBuf, int len, int nMsgType) { stu_msgbuf_t stuSendMsg = {0}; int ret = -1; stuSendMsg.msgtype = nMsgType; ret = msgrcv(msgid, &stuSendMsg, sizeof(stuSendMsg.msgtext), stuSendMsg.msgtype ,IPC_NOWAIT); if(-1 == ret) { printf("\n msgrcvg s_nClientMsgid [%d] errno=%d [%s]\n", msgid ,errno,strerror(errno)); return -1; } else { printf("\n msgrcv [%s]\n",stuSendMsg.msgtext); } return 0; } int init(int *pMsgId) { int msgid = -1; key_t ktmp = ftok("/share/1.tmp",lientKey); if(ktmp == -1) { printf("\n if(ktmp == -1) errno=%d [%s]\n",errno,strerror(errno)); return -1; } else { printf("\n ftok ktmp[%d]\n",ktmp); } msgid = msgget(ktmp, 0666|IPC_CREAT); if(msgid < 0) { printf("\n if(gs_nClientMsgid == -1) errno=%d [%s]\n",errno,strerror(errno)); return -1; } printf("\n msgget sucess %d ktmp[%d]\n",msgid,ktmp); *pMsgId = msgid; return 0; } int main() { int ret =-1; int msgid =-1; ret = init(&msgid); if(ret == -1) { return 0; } while(1) { char chStr[20] = {0}; int msgtype = 2; //printf("\ni[%d]\n",i); //snprintf(chStr,sizeof(chStr),"%d --- %ld",i,time(NULL)); rcvmsg(msgid,chStr, strlen(chStr)+1,msgtype); sleep(2); } }
由此可见,服务端只接收了类型2。