消息队列综合案例

消息队列实现回射客户/服务器

 

server进程接收时, 指定msgtyp为0, 从队首不断接收消息

server进程发送时, 将mtype指定为接收到的client进程的pid

client进程发送的时候, mtype指定为自己进程的pid

client进程接收时, 需要将msgtyp指定为自己进程的pid, 只接收消息类型为自己pid的消息;

// client/server进程接收/发送的数据结构
const int MSGMAX = 8192;
struct msgBuf
{
    long mtype;         //保存客户进程的pid(需要将pid强制转换成为long)
    char mtext[MSGMAX]; //保存客户进程真实发送的数据
};
//server.cpp
void echoServer(int msgid)
{
    struct msgBuf buf;
    int nrcv;
    while (true)
    {
        bzero(&buf, sizeof(buf));
        if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)
            err_exit("msgrcv error");
        cout << "recv: " << buf.mtext;
        if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)
            err_exit("msgsnd error");
    }
}

int main()
{
    key_t key = ftok("/tmp/echoSeed", 0x1234);
    int msgid = msgget(key, IPC_CREAT|0666);
    if (msgid == -1)
        err_exit("msgget error");

    echoServer(msgid);
}
//client.cpp
void echoServer(int msgid)
{
    struct msgBuf buf;
    int nrcv;
    while (true)
    {
        bzero(&buf, sizeof(buf));
        if ((nrcv = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0)) == -1)
            err_exit("msgrcv error");
        cout << "recv: " << buf.mtext;
        if (msgsnd(msgid, &buf, strlen(buf.mtext), 0) == -1)
            err_exit("msgsnd error");
    }
}

int main()
{
    key_t key = ftok("/tmp/echoSeed", 0x1234);
    int msgid = msgget(key, IPC_CREAT|0666);
    if (msgid == -1)
        err_exit("msgget error");

    echoServer(msgid);
}

附-ftok用法

#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *pathname, int proj_id);

描述信息:

The ftok() function uses the identity(象征) of the file named by the given pathname (which must refer

to an existing, accessible file[必须是一个已经存在,并且可访问的文件]) and the least significant(有效的) 8 bits[有效的最低8位] of proj_id (which must  be  nonzero)  to  generate  a  key_t  type  System V IPC key, suitable

for use with msgget(2), semget(2), or shmget(2).   The resulting value is the same for all pathnames that name the same file, when the  same value  of  proj_id

is used(如果文件名与proj_id的有效位全都相同的话, 则生成的key一定也是相同的).  The value returned should be different when

the (simultaneously existing) files or the project IDs differ.

RETURN VALUE   On success, the generated key_t value is returned.  On failure -1 is returned,

with errno indicating the error as for the stat(2) system call.

最新文章

  1. CentOS 7.0系统安装配置步骤详解
  2. UNITY5以后怎么改GUI文字
  3. EnTaroTassadar
  4. python学习-day12:列表、元祖、字典介绍和内置
  5. 了解 XSS 攻击原理
  6. java 输入输出流1 FileInputStrem&amp;&amp;FileOutStream
  7. cocos2dx-lua class语法糖要注意了
  8. 关于wordpress在修改固定链接后,总显示Not Found的问题
  9. bzoj 1088: [SCOI2005]扫雷Mine
  10. Linq to Sql : 三种事务处理方式
  11. .net面试题【持续更新.....】
  12. Linux查询已开启文件或已运行进程开启之文件fuser,lsof,pidof
  13. 几何学观止(Lie群部分)
  14. dyld环境变量
  15. Java NIO系列教程(四) Scatter/Gather
  16. beta2
  17. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #17 如何使用ext4
  18. linux centos7 安装git
  19. MongoDB: 原子性和事务
  20. JavaScript实现的3D球面标签云效果

热门文章

  1. CentOS6.8虚拟机安装及ORALCE安装记录
  2. Microsoft SQL server2017初次安装与使用记录
  3. R语言集合操作
  4. Docker配置文件
  5. 一个未排序整数数组,有正负数,重新排列使负数排在正数前面,并且要求不改变原来的正负数之间相对顺序,比如: input: 1,7,-5,9,-12,15 ans: -5,-12,1,7,9,15 要求时
  6. 每CPU变量
  7. Linux-2.6.25 TCPIP函数调用大致流程
  8. 剑指Offer——联通研究院笔、面试题 (Offer已收割)
  9. android 图片网络下载github开源框架之Universal-Image-Loader
  10. 03_dbcp数据源依赖jar包,DBCP中API介绍,不同过dbcp方式使用dbcp数据库连接池,通过配置文件使用dbcp数据库连接池