消息队列的应用实例

以一个聊天程序为例:

聊天的两端分别为进程server和进程client

server:

 

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>





#define PROJ_ID  11
#define BUF_SIZE 256
#define EXIT_CODE 1
#define MSND_TYPE  1
#define RCV_TYPE   2

struct mymsgbuf{
    long msgtype;
    char buf[BUF_SIZE];
};


int main()
{
    struct mymsgbuf msgbuf;
    key_t key;
    int pid;
    int len;
    
    bzero(&msgbuf, sizeof(msgbuf));
    len = sizeof(msgbuf) - 4;

    key = ftok(".", PROJ_ID);
    if (-1 == key)
    {
        perror("ftok error:");
        exit(EXIT_CODE);
    }

    pid = msgget(key, IPC_CREAT|0660);
    if (-1 == pid)
    {
        perror("msgget error:");
        exit(EXIT_CODE);
    }

    

    while(1)
    {
        bzero(&msgbuf, sizeof(msgbuf));
        printf("server: ");
        fgets(msgbuf.buf, BUF_SIZE, stdin);

        
        msgbuf.buf[strlen(msgbuf.buf)-1] = '\0';
        msgbuf.msgtype = MSND_TYPE;
        len = sizeof(msgbuf) - 4;
        if (-1 == msgsnd(pid, &msgbuf, len, 0))
        {
            perror("msgsnd error:");
            break;
        }

        if ( strncmp("exit", msgbuf.buf, strlen("exit")) == 0)
        {
            sleep(1);
            break;
        }

        bzero(&msgbuf, sizeof(msgbuf));
        if (-1 == msgrcv(pid, &msgbuf, BUF_SIZE, RCV_TYPE, 0))
        {
            perror("msgrcv error:");
            break;
        }
        
        printf("recv from client message:%s\n", msgbuf.buf);
        
    }

    msgctl(pid, IPC_RMID, NULL);
    
    return 0;
}

 

 

 

client:

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>





#define PROJ_ID  11
#define BUF_SIZE 256
#define EXIT_CODE 1
#define MSND_TYPE  2
#define RCV_TYPE   1

struct mymsgbuf{
    long msgtype;
    char buf[BUF_SIZE];
};


int main()
{
    struct mymsgbuf msgbuf;
    key_t key;
    int pid;
    int len;
    
    bzero(&msgbuf, sizeof(msgbuf));
    len = sizeof(msgbuf) - 4;

    key = ftok(".", PROJ_ID);
    if (-1 == key)
    {
        perror("ftok error:");
        exit(EXIT_CODE);
    }

    pid = msgget(key, IPC_CREAT|0660);
    if (-1 == pid)
    {
        perror("msgget error:");
        exit(EXIT_CODE);
    }

    

    while(1)
    {

        bzero(&msgbuf, sizeof(msgbuf));
        if (-1 == msgrcv(pid, &msgbuf, BUF_SIZE, RCV_TYPE, 0))
        {
            perror("msgrcv error:");
            break;
        }

        printf("recv from server message:%s\n", msgbuf.buf);

        if ( strncmp("exit", msgbuf.buf, strlen("exit")) == 0)
        {
            break;
        }

        bzero(&msgbuf, sizeof(msgbuf));
        printf("client: ");
        fgets(msgbuf.buf, BUF_SIZE, stdin);
        if ( strncmp("exit", msgbuf.buf, strlen("exit")) == 0)
        {
            break;
        }
        
        msgbuf.buf[strlen(msgbuf.buf)-1] = '\0';
        msgbuf.msgtype = MSND_TYPE;
        if (-1 == msgsnd(pid, &msgbuf, strlen(msgbuf.buf)+1, 0))
        {
            perror("msgsnd error:");
            break;
        }
        
        
    }

    
    
    return 0;
}

 

转载于:https://www.cnblogs.com/zhangxuan/p/6732573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值