zoukankan      html  css  js  c++  java
  • 消息队列的应用实例

    以一个聊天程序为例:

    聊天的两端分别为进程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] = '';
            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
    ", 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
    ", 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] = '';
            msgbuf.msgtype = MSND_TYPE;
            if (-1 == msgsnd(pid, &msgbuf, strlen(msgbuf.buf)+1, 0))
            {
                perror("msgsnd error:");
                break;
            }
            
            
        }
    
        
        
        return 0;
    }
  • 相关阅读:
    python paramiko模块学习分享
    中国大数据市场规模分析及预测
    中国大数据市场规模分析及预测
    显著性水平 置信度 置信区间 实例讲解
    显著性水平 置信度 置信区间 实例讲解
    加密算法
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'qingmu' for key 'PRIMARY'
    spring Security的自定义用户认证
    spring的面试题
    solr和ElasticSearch(ES)的区别?
  • 原文地址:https://www.cnblogs.com/zhangxuan/p/6732573.html
Copyright © 2011-2022 走看看