Linux Message Queue Demo

Client:

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <mqueue.h>
#include <unistd.h>
#include <stdio.h>

#define MSG_SERVER "/msgqueue"
#define BUF_LEN 20000

int main(int argc,char** argv)
{
    mqd_t   qd;
    int     len;
    ssize_t rec_len;
    char    rec_buf[BUF_LEN];
    char    *p = NULL;

memset(rec_buf, 1, sizeof(rec_buf));

/*
     * Opn message queue
     */
    if ( (qd = mq_open(MSG_SERVER, O_RDONLY, 0, NULL)) < 0)
    {
        printf("open msg queue error.\n");
        exit(-1);
    }

/*
     * Read msg
     */
    while (1)
    {
        if ( (rec_len = mq_receive(qd, rec_buf, sizeof(rec_buf), 0)) < 0)
        {
            printf("Receive msg error.\n");
            exit(-1);
        }
        else
        {
            printf("Receive msg successfully.\n");
            for (p = rec_buf; *p != ‘\0‘ && p != (rec_buf + rec_len); ++p)
            {
                printf("%c", *p);
            }
        }

sleep(2);
    }

mq_close(qd);
    mq_unlink(MSG_SERVER);
    exit(0);
}

Server:

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <mqueue.h>
#include <stdio.h>
#include <unistd.h>

#define MSG_SERVER "/msgqueue"
#define BUF_LEN 100

int main(int argc, char** argv)
{
    mqd_t   qd;
    struct mq_attr *attr;
    mode_t  access;
    char    send_buf[BUF_LEN];

access = S_IRUSR | S_IWUSR | S_IROTH;

if ( (attr = (struct mq_attr *)malloc(sizeof(struct mq_attr))) == NULL)
    {
        printf("Alloc memory error!\n");
        exit(-1);
    }
    memset(attr, 0, sizeof(struct mq_attr));

memset(send_buf, 1, sizeof(send_buf));
    send_buf[BUF_LEN - 1] = ‘\0‘;

/*
     * Create message queue
     */

if ( (qd = mq_open(MSG_SERVER, O_RDWR | O_CREAT | O_EXCL, access, NULL)) < 0)
    {
        printf("open msg queue error.\n");
        exit(-1);
    }

/*
     * Get default attr
     */
    printf("Get message queue default attr:\n");
    if (mq_getattr(qd, attr) < 0)
    {
       printf("Get attr error.\n");
    }
    else if (attr != NULL)
    {
       printf("Default msg count:%ld, msgsize:%ld \n", attr->mq_maxmsg, attr->mq_msgsize);
    }
    free(attr);

/*
     * Send msg
     */
    while (1)
    {
        if (mq_send(qd, send_buf, sizeof(send_buf), 0) < 0)
        {
            printf("Send msg error.\n");
            exit(-1);
        }
        else
        {
            printf("Send msg successfully.\n");
        }

sleep(2);
    }

mq_close(qd);
    mq_unlink(MSG_SERVER);
    exit(0);
}

时间: 2024-07-30 20:13:58

Linux Message Queue Demo的相关文章

基于System V Message queue的PHP消息队列封装

原创文章,转载请注明出处:http://www.huyanping.cn/?p=235 作者:Jenner System V Message queue 是一种进程通信(IPC)的方式,方便实现生产者-消费者模型,单个或多个生产者向队列中写入消息,多个生产者再从队列中获取消息进行处理. 项目地址:https://github.com/huyanping/Zebra-PHP-Framework 该Wrapper支持: 进程通信 设置最大队列容量(字节单位) 获取当前队列数量 修改队列部分属性 注意

MSMQ(Microsoft Message Queue)

http://www.cnblogs.com/sk-net/archive/2011/11/25/2232341.html 利用 MSMQ(Microsoft Message Queue),应用程序开发人员可以通过发送和接收消息方便地与应用程序进行快速可靠的通信.消息处理为您提供了有保障的消息传递和执行许多业务处理的可靠的防故障方法. MSMQ与XML Web Services和.Net Remoting一样,是一种分布式开发技术.但是在使用XML Web Services或.Net Remot

Android开发:Handler异步通信机制全面解析(包含Looper、Message Queue

前言 最近刚好在做关于异步通信的需求,那么,今天我们来讲解下Android开发中的Handler异步通信传递机制(包括Looper.Message Queue) 目录 定义 Android提供的一套消息传递机制 作用 用于实现子线程对UI线程的更新,实现异步消息的处理: - 在新启动的线程中发送消息 - 在主线程中获取并处理信息 为什么要用Handler 在安卓开发中: - 为了保证Android的UI操作是线程安全的,Android规定了只允许UI线程修改Activity里的UI组件: - 但

HDU 1509 Windows Message Queue

题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the qu

HDU 1509:Windows Message Queue【优先队列】

Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4505    Accepted Submission(s): 1794 Problem Description Message queue is the basic fundamental of windows system. For each

Message Queue基本使用说明

一.安装Message Queue: 在Win7之前,控制面板,添加删除组件(Windows Message Queue). Win7~Win8:控制面板,程序和功能,启用或关闭Windows功能(找到Windows Message Queue服务器)选项,连同所有子类一并勾上即可,自动安装. 二.使用Message Queue: 1)用于各类服务器.计算机之间的通讯: 本地,自己给自己发(直接是.\\Private$\\Queue的私有名字). 远程计算机: FormatName:Direct

在单线程模型中 Message、Handler、Message Queue、Looper 之间的关系

Message,信息的载体,用来传递数据给Handler. Handler (Handler处理者,是 Message 的主要处理者,负责 Message 的发送,Message 内容的执行处理)发送和处理Message和Runable对象,这些对象和一个线程的MessageQueue相关联.每一个线程实例和一个单独的线程以及该线程的 MessageQueue 相关联.Handler和创建它的线程绑定在一起,把 Message和Runable 对象传递给 MessageQueue,这些对象离开

ZOJ2724_Windows Message Queue(STL/优先队列)

解题报告 题意: 看输入输出就很明白. 思路: 优先队列. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; struct node

linux tcp server demo

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <sys/types.h>  #include <sys/socket.h>  #include <string.h>  #include <netin