• linux IPC 消息队列(二)


    我在网上想找多进程之间的通信方式,发现有人写的消息队列很好,搬过来:

    common.h

     1 #ifndef __COMMON_H_
     2 #define __COMMON_H_
     3 
     4 #include <stdio.h>
     5 #include <unistd.h>
     6 #include <stdlib.h>
     7 #include <sys/ipc.h>
     8 #include <sys/msg.h>
     9 #include <sys/types.h>
    10 #include <string.h>
    11 #include <time.h>
    12 
    13 #define MSG_SIZE 1024
    14 #define FILEPATH "."
    15 #define ID       0
    16 #define SERVER_TYPE 1
    17 #define CLIENT_TYPE 2
    18 
    19 typedef struct msg_info {
    20         long mtype;
    21         char mtext[MSG_SIZE];
    22 }msginfo;
    23 
    24 int CreateMessageQueue();
    25 int GetMessageQueue();
    26 int DeleteMessageQueue(int msgid);
    27 int SendDataToMessageQueue(int msg_id, int send_type, char *msg);
    28 int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out);
    29 
    30 #endif

    common.c

     1 #include "common.h"
     2 
     3 static int CommonMessageQueue(int flags)
     4 {
     5         key_t _key = ftok(FILEPATH, ID);
     6         if(_key == -1) {
     7                 perror("ftok error");
     8                 return 1;
     9         }
    10         int _msg_id = msgget(_key, flags);
    11         if(_msg_id < 0) {
    12                 perror("msgget error");
    13                 return 2;
    14         }
    15         return _msg_id;
    16 }
    17 
    18 int CreateMessageQueue()
    19 {
    20         return CommonMessageQueue(IPC_CREAT|IPC_EXCL|0666);
    21 }
    22 
    23 int GetMessageQueue()
    24 {
    25         return CommonMessageQueue(IPC_CREAT);
    26 }
    27 
    28 int DeleteMessageQueue(int msg_id)
    29 {
    30         if(msgctl(msg_id, IPC_RMID, NULL) < 0)
    31                 return -1;
    32         return 0;
    33 }
    34 
    35 int SendDataToMessageQueue(int msg_id, int send_type, char *msg)
    36 {
    37         msginfo buff;
    38         buff.mtype = send_type;
    39         strcpy(buff.mtext, msg);
    40         int msg_snd = msgsnd(msg_id, (void *)&buff, sizeof(buff), 0);
    41         if(msg_snd < 0) {
    42                 perror("msgsnd error");
    43                 return -3;
    44         }
    45         return 0;
    46 }
    47 
    48 int ReceiveDataFromMessageQueue(int msg_id, int receive_type, char *out)
    49 {
    50         msginfo buff;
    51         int msg_rcv = msgrcv(msg_id, (void *)&buff, sizeof(buff), receive_type, 0);
    52         if(msg_rcv < 0) {
    53                 perror("msg_rcv error");
    54                 return -4;
    55         }
    56         strcpy(out, buff.mtext);
    57         return 0;
    58 }

    server.c

     1 #include "common.h"
     2 
     3 int main()
     4 {
     5         char buff[MSG_SIZE];
     6         int msg_id = CreateMessageQueue();
     7 
     8         while(1)
     9         {
    10                 //send data
    11                 printf("server please enter# ");
    12                 fflush(stdout);
    13                 ssize_t s = read(0, buff, sizeof(buff)-1);
    14                 if(s > 0) {
    15                         buff[s-1] = 0;
    16                         SendDataToMessageQueue(msg_id, SERVER_TYPE, buff);
    17                         printf("data has sended,wait receive......
    ");
    18                 } else {
    19                         perror("read error");
    20                         return 1;
    21                 }
    22 
    23                 //receive data
    24                 ReceiveDataFromMessageQueue(msg_id, CLIENT_TYPE, buff);
    25                 printf("from client: %s
    ", buff);
    26         }
    27         DeleteMessageQueue(msg_id);
    28 
    29         return 0;
    30 }

    client:

     1 #include "common.h"
     2 
     3 int main()
     4 {
     5         char buff[MSG_SIZE];
     6         int msg_id = GetMessageQueue();
     7         while(1) {
     8                 //receive data
     9                 ReceiveDataFromMessageQueue(msg_id, SERVER_TYPE, buff);
    10                 printf("from server:%s
    ", buff);
    11 
    12                 //send data
    13                 printf("client please enter# ");
    14                 fflush(stdout);
    15                 ssize_t s = read(0, buff, sizeof(buff)-1);
    16                 if(s <= 0) {
    17                         perror("read error");
    18                         return 1;
    19                 } else {
    20                         buff[s-1] = 0;
    21                         SendDataToMessageQueue(msg_id, CLIENT_TYPE, buff);
    22                         printf("data has sended,wait receive......
    ");
    23                 }
    24         }
    25         return 0;
    26 }

    Makefile:

    all:client server
    
    client: common.c client.c
            gcc -o $@ $^
    server: common.c server.c
            gcc -o $@ $^
    
    .PHONY:clean
    clean:
            rm -rf client server
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    如何让xcode自动检查内存泄露
    Property's synthesized getter follows Cocoa naming convention for returning
    Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'Info
    iPhone开发过程中调试多次Release问题 message sent to deallocated
    委托 详解
    UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直观详解(扩展)
    代码书写规范(摸索、试行)
    简述 IOS中的LazyLoad思想
    UIImageView 详解
    20140413 科技脉搏-风平浪静,默默成长
  • 原文地址:https://www.cnblogs.com/ch122633/p/8471887.html
Copyright © 2020-2023  润新知