消息队列综合案例

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

 

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. C++ 程序在运行时不显示dos界面

    在程序最开始处加一句: #pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) PS: 在VS中 ...

  2. Python之禅及其翻译

    凡是用过 Python的人,基本上都知道在交互式解释器中输入 import this 就会显示 Tim Peters 的 The Zen of Python,但它那偈语般的语句有点令人费解,所以我想分 ...

  3. python解析json文件之简介

    一.JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition ...

  4. 牛客网编程练习之PAT乙级(Basic Level):1033 害死人不偿命的(3n+1)猜想

    3n+1水题.... AC代码: import java.util.Scanner; /** * @author CC11001100 */ public class Main { public st ...

  5. ACM Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  6. ACM Adding Reversed Numbers(summer2017)

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...

  7. C实战:项目构建Make,Automake,CMake

    C实战:项目构建Make,Automake,CMake 在本系列文章<C实战:强大的程序调试工具GDB>中我们简要学习了流行的调试工具GDB的使用方法.本文继续"C实战" ...

  8. Objective-C数据结构

    Objective-C数据结构 枚举 typedef enum { SexMan, SexWoman } Sex; 结构体 typedef struct { int year; int month; ...

  9. Dynamics CRM Entity Relationship Many to Many (N:N)

    该博客对N:N的关系的查询列出了两种方式,一种RetrieveMultipleRequest,一种Fetch XML ,有谁对N:N关系的查询了解不是很深的可以学习下. http://andreasw ...

  10. Android的5层平台架构

    Android 是一种基于 Linux 的开放源代码软件栈,为广泛的设备和机型而创建.下图所示为 Android 平台的主要组件. Android 软件栈 Linux 内核 Android 平台的基础 ...