#ifndef _WBM_MY_MSG_H_
#define _WBM_MY_MSG_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/msg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MSGERR_BASE 200
#define MSGERR_PARAM (MSGERR_BASE + 1)
#define MSGERR_EEXISTS (MSGERR_BASE + 2)
#define DATALEN 1024*10
typedef struct _msg
{
long mtype;
char p[DATALEN];
} MSG;
/**
* [msg_create 创建消息队列]
* @param key [key 值]
* @param msgid [out 消息队列的标识符]
* @return []
*/
int msg_create(int key, int *msgid);
/**
* [msg_open 打开消息队列]
* @param key [key 值]
* @param msgid [out 消息队列的标识符]
* @return []
*/
int msg_open(int key, int *msgid);
/**
* [msg_send 发送信息]
* @param msgid [消息队列的标识符]
* @param buf [in 带发送内容地址]
* @param butlen [发送信息体长度]
* @param msgtype [消息的类型]
* @param flag [函数的控制属性]
* @return []
*/
int msg_send(int msgid, char *buf, int butlen, int msgtype, int flag);
/**
* [msg_recv 接收信息]
* @param msgid [消息队列的标识符]
* @param buf [接收信息体的地址]
* @param butlen [信息长度]
* @param msgtype [消息的类型]
* @param flag [函数的控制属性]
* @return []
*/
int msg_recv(int msgid, char *buf, int *butlen, int msgtype, int flag);
/**
* [msg_del 消息队列删除]
* @param msgid [消息队列的标识符]
* @return []
*/
int msg_del(int msgid);
#ifdef __cplusplus
}
#endif
#endif
//源文件msgipc_msg.c
int msg_create(int key, int *msgid)
{
int ret = 0;
int tmpid = 0;
if(msgid == NULL)
{
ret = MSGERR_PARAM;
printf("fun msg_create() err:%d\n", ret);
return ret;
}
tmpid = msgget(key, 0666 | IPC_CREAT | IPC_EXCL);
if(msgid < 0)
{
ret = errno;
printf("fun msg_create() err:%d\n", ret);
return ret;
}
*msgid = tmpid;
return ret;
}
int msg_open(int key, int *msgid)
{
int ret = 0;
int tmpid = 0;
if(msgid == NULL)
{
ret = MSGERR_PARAM;
printf("fun msg_open() err:%d\n", ret);
return ret;
}
tmpid = msgget(key, 0666);
if(tmpid < 0)
{
ret = errno;
printf("fun msg_open() err:%d\n", ret);
return ret;
}
*msgid = tmpid;
return ret;
}
int msg_send(int msgid, char *buf, int butlen, int msgtype, int flag)
{
MSG msg;
int ret = 0;
if(buf == NULL)
{
ret = MSGERR_PARAM;
printf("fun msg_send() err:%d\n", ret);
return ret;
}
memset(&msg, 0, sizeof(MSG));
msg.mtype = msgtype;
strcpy(msg.p, buf); // 正文内容
ret = msgsnd(msgid, &msg, butlen, flag);
if(ret < 0)
{
ret = errno;
printf("fun msgsnd() err:%d---butlen=%d\n", ret, butlen);
return ret;
}
return ret;
}
int msg_recv(int msgid, char *buf, int *butlen, int msgtype, int flag)
{
int ret = 0;
if(buf == NULL || butlen == NULL)
{
ret = MSGERR_PARAM;
printf("fun msg_recv() err:%d\n", ret);
return ret;
}
MSG msg;
memset(&msg, 0, sizeof(MSG));
ret = msgrcv(msgid, &msg, sizeof(msg.p), msgtype, flag);
if(ret < 0)
{
ret = errno;
printf("fun msgrcv() err:%d\n", ret);
return ret;
}
memcpy(buf, msg.p, ret);
*butlen = ret;
ret = 0;
return ret;
}
int msg_del(int msgid)
{
int ret = 0;
ret = msgctl(msgid, IPC_RMID, NULL);
if(ret == -1)
{
ret = errno;
printf("fun msg_del() err:%d\n", ret);
return ret;
}
return ret;
}
/*************************************************************************
> File Name: main.c
> Author: pog
> Mail: 10864315@qq.com
> Created Time: 2017-01-15 19:40:06
************************************************************************/
#include "msgipc_msg.h"
int main(int argc, char *argv[])
{
int ret = 0;
int key = 0x1234;
int msgid;
ret = msg_create(key, &msgid);
if(ret)
return ret;
ret = msg_send(msgid, "1234567890", 5, 10, 0);
if(ret)
return ret;
int len = 100;
char buf[100] = {0};
ret = msg_recv(msgid, buf, &len, 10, 0);
if(ret)
return ret;
printf("recv = %s\n", buf);
msg_del(msgid);
return 0;
}
下载