参考 linux 网络编程第二版 敲

发现 出错了... ...
网上也没有特别多的参考程序。后来在 man msgrcv 里面找到了参考程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msgbuf {
   long mtype;
   char mtext[80];
};

static void
usage(char *prog_name, char *msg)
{
   if (msg != NULL)
       fputs(msg, stderr);

   fprintf(stderr, "Usage: %s [options]\n", prog_name);
   fprintf(stderr, "Options are:\n");
   fprintf(stderr, "-s        send message using msgsnd()\n");
   fprintf(stderr, "-r        read message using msgrcv()\n");
   fprintf(stderr, "-t        message type (default is 1)\n");
   fprintf(stderr, "-k        message queue key (default is 1234)\n");
   exit(EXIT_FAILURE);
}

static void
send_msg(int qid, int msgtype)
{
   struct msgbuf msg;
   time_t t;
  msg.mtype = msgtype;

   time(&t);
   snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
           ctime(&t));

   if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
               IPC_NOWAIT) == -1) {
       perror("msgsnd error");
       exit(EXIT_FAILURE);
   }
   printf("sent: %s\n", msg.mtext);
}

static void
get_msg(int qid, int msgtype)
{
   struct msgbuf msg;

   if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
              MSG_NOERROR | IPC_NOWAIT) == -1) {
       if (errno != ENOMSG) {
           perror("msgrcv");
           exit(EXIT_FAILURE);
       }
       printf("No message available for msgrcv()\n");
   } else
       printf("message received: %s\n", msg.mtext);
}

int
main(int argc, char *argv[])
{
   int qid, opt;
   int mode = 0;               /* 1 = send, 2 = receive */
   int msgtype = 1;
   int msgkey = 1234;

   while ((opt = getopt(argc, argv, "srt:k:")) != -1) {
     switch (opt) {
       case 's':
           mode = 1;
           break;
       case 'r':
           mode = 2;
           break;
       case 't':
           msgtype = atoi(optarg);
           if (msgtype <= 0)
               usage(argv[0], "-t option must be greater than 0\n");
           break;
       case 'k':
           msgkey = atoi(optarg);
           break;
       default:
           usage(argv[0], "Unrecognized option\n");
       }
   }

   if (mode == 0)
       usage(argv[0], "must use either -s or -r option\n");

   qid = msgget(msgkey, IPC_CREAT | 0666);

   if (qid == -1) {
       perror("msgget");
       exit(EXIT_FAILURE);
   }

   if (mode == 2)
       get_msg(qid, msgtype);
   else
       send_msg(qid, msgtype);

exit(EXIT_SUCCESS);
}

首先一定要会用errno

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <sys/ipc.h>
void msg_show_attr(int msg_id, struct msqid_ds msg_info)
{
    int ret = -1;
    sleep(1);
    ret = msgctl(msg_id, IPC_STAT, &msg_info);
    if(-1 == ret)
    {
        printf("获取消息失败   ***** %d \n",errno);
        return ;
    }
    printf("\n");

    printf("现在队列中的字节数:%ld\n",msg_info.msg_cbytes);

    printf("队列中的消息数:%d\n",(int)msg_info.msg_qnum);

    printf("队列中最大的字节数:%d\n",(int)msg_info.msg_qbytes);

    printf("最后发送消息的进程pid: %d\n",msg_info.msg_lspid);

    printf("最后接受消息的进程pid: %d\n",msg_info.msg_lrpid);

    printf("最后发送消息的时间:%s",ctime(&(msg_info.msg_stime)));

    printf("最后接受消息的时间:%s",ctime(&(msg_info.msg_rtime)));

    printf("最后变化的时间:%s",ctime(&(msg_info.msg_ctime)));

    printf("消息UID是: %d\n",msg_info.msg_perm.uid);

    printf("消息GID是: %d\n",msg_info.msg_perm.gid);


}

int main()
{
    int ret = -1;
    int msg_flags, msg_id;
    key_t key;
    int temp;
    struct msgmbuf
    {
        long mtype;
        char mtext[10];
    };
    struct msqid_ds msg_info;
    struct msgmbuf msg_mbuf;

    int msg_sflags,msg_rflags;
    char *msgpath = "/ipc/msg/";
    key = ftok(msgpath, 'X');
    if(key != -1)
    {
        printf("成功建立\n");
    }
    else
    {
        printf("建立失败\n");
    }
    msg_flags = IPC_CREAT|IPC_EXCL;
    msg_id = msgget(key, msg_flags|0777);
    if(-1 == msg_id)
    {
        printf("消息建立失败\n");
        return 0;
    }
    msg_show_attr(msg_id, msg_info);

    msg_sflags = IPC_NOWAIT;
    msg_mbuf.mtype = 220;
    memcpy(msg_mbuf.mtext, "abcdef", sizeof("abcdef"));
    ret = msgsnd(msg_id, (void *)&msg_mbuf, sizeof("abcdef"),msg_sflags);

    if(-1 == ret)
    {
        printf("发送消息失败\n");
    }
    msg_show_attr(msg_id, msg_info);
    msg_rflags = IPC_NOWAIT|MSG_NOERROR;
    temp = sizeof(struct msgmbuf) - sizeof(long);
    ret = msgrcv(msg_id, (void *)&msg_mbuf,temp, 220,msg_rflags);

    if(ret == -1)
    {
        printf("接收消息失败\n");
        printf("%d***",errno);
    }
    else
    {
        printf("接受消息成功长度是: %d\n",ret);
    }
    msg_show_attr(msg_id, msg_info);

    msg_info.msg_perm.uid = 1000;
    msg_info.msg_perm.gid = 1000;
    //msg_info.msg_qbytes = 120;
    ret = msgctl(msg_id, IPC_SET,&msg_info);// 设置消息属性

    if(ret == -1)
    {
        printf("设置消息属性失败\n");
        return 0;
    }
    msg_show_attr(msg_id, msg_info);

    ret = msgctl(msg_id, IPC_RMID, NULL);
    if(-1 == ret)
    {
        printf("删除消息失败\n");
        return 0;
    }
    return 0;
}

后来修了一个版本 首先 那个消息结构体一定要用long,如果不用long的话。就会一直接收失败errno的意思大概是,没有找到对应的消息类型。
还有一个问题没有解决,就是修改属性是成功的可以,然后就不能成功读取msg_info消息的值,不清楚到底是什么原因。如果有人知道,请留言。。。。。。
errno错误大全

posted on 2017-11-22 22:36  HDU李少帅  阅读(155)  评论(0编辑  收藏  举报