消息队列综合案例

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

 

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.

Linux IPC实践(6) --System V消息队列(3)的更多相关文章

  1. Linux IPC实践(4) --System V消息队列(1)

    消息队列概述 消息队列提供了一个从一个进程向另外一个进程发送一块数据的方法(仅局限于本机); 每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值. 消息队列也有管道一样的不足:  ...

  2. Linux IPC实践(5) --System V消息队列(2)

    消息发送/接收API msgsnd函数 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 参数 msgid: 由ms ...

  3. Linux进程通信之System V消息队列

    System V消息队列是Open Group定义的XSI,不属于POSIX标准.System V IPC的历史相对很早,在上个世70年代后期有贝尔实验室的分支机构开发,80年代加入System V的 ...

  4. linux c编程:System V消息队列一

    消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足 够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进 ...

  5. linux网络编程之system v消息队列(二)

    今天继续学习system v消息队列,主要是学习两个函数的使用,开始进入正题: 下面则开始用代码来使用一下该发送函数: 在运行之前,先查看一下1234消息队列是否已经创建: 用上次编写的查看消息队列状 ...

  6. Linux IPC实践(13) --System V IPC综合实践

    实践:实现一个先进先出的共享内存shmfifo 使用消息队列即可实现消息的先进先出(FIFO), 但是使用共享内存实现消息的先进先出则更加快速; 我们首先完成C语言版本的shmfifo(基于过程调用) ...

  7. Linux IPC实践(11) --System V信号量(1)

    信号量API #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semget ...

  8. Linux IPC实践(9) --System V共享内存

    共享内存API #include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int ...

  9. linux网络编程之system v消息队列(一)

    经过上次对于进程通讯的一些理论的认识之后,接下来会通过实验来进一步加深对进程通讯的认识,话不多说,进入正题: 其实还可以通过管道,但是,管道是基于字节流的,所以通常会将它称为流管道,数据与数据之间是没 ...

随机推荐

  1. HTMLParser使用简介

    HTMLParser具有小巧,快速的优点,缺点是相关文档比较少(英文的也少),很多功能需要自己摸索.对于初学者还是要费一些功夫的,而一旦上手以后,会发现HTMLParser的结构设计很巧妙,非常实用, ...

  2. 监控undo空间和临时段的使用情况

    --1.监控undo空间情况 ),) free_space from dba_free_space where tablespace_name='UNDOTBS1' group by tablespa ...

  3. 数据结构Java版之交换算法(一)

    交换的本质是拷贝,其中拷贝包括两种方式.值拷贝和指针拷贝,在java中没有指针,为此,我们可以理解为地址拷贝,在我看来,指针就是地址. 1.传值方式示例: 由上述示例可得,传值,不能起到交换的作用,原 ...

  4. ACM Secrete Master Plan

    Problem Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. T ...

  5. Python3 注释

    确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以#开头,例如: #!/usr/bin/python3 #coding=utf-8 ...

  6. API得到Windows版本

    API得到Windows版本 /** * Windows Version * https://msdn.microsoft.com/en-us/library/windows/desktop/dn48 ...

  7. POSIX 消息队列相关问题

    一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...

  8. Java经典设计模式之十一种行为型模式(附实例和详解)

    Java经典设计模式共有21中,分为三大类:创建型模式(5种).结构型模式(7种)和行为型模式(11种). 本文主要讲行为型模式,创建型模式和结构型模式可以看博主的另外两篇文章:Java经典设计模式之 ...

  9. HBase的环境配置及其应用

    -------------------------------------------------------------------------------------- [版权申明:本文系作者原创 ...

  10. Python descriptor

    class A: def __init__(self, name): self.name = name def __get__(self, ins, cls): print('call get') i ...