MsgLocalServer.c 912 Bytes
Newer Older
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
#include <sys/ipc.h>
#include <stdint.h>
#include <stdio.h>
#include <error.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <string.h>
#include "../lib/sock_msg.h"

int main(int argc, char *argv[])
{
    msg_data_t msg;

    // 初始化消息队列
    if (init_msg_queue() == -1)
    {
        exit(EXIT_FAILURE);
    }

    while (1)
    {
        if (msg_local_recv(&msg) > 0)
        {
            printf("Received code: %d\n", msg.code);
            printf("Received type: %d\n", msg.type);
            printf("Received len: %d\n", msg.len);
            printf("Received pad: %s\n", (char *)(msg.pad));
            // 清空pad
            memset(msg.pad, 0, msg.len);
        }
        else
        {
            destroy_msg_queue();
            exit(EXIT_FAILURE);
        }
    }

    // 清理消息队列
    if (destroy_msg_queue() == -1)
    {
        exit(EXIT_FAILURE);
    }

    return 0;
}