溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于消息隊(duì)列的雙向通信

發(fā)布時(shí)間:2020-07-03 10:14:15 來源:網(wǎng)絡(luò) 閱讀:345 作者:小楊楊雪松 欄目:編程語(yǔ)言

       消息隊(duì)列提供了一種一個(gè)進(jìn)程向另一個(gè)進(jìn)程發(fā)送一個(gè)數(shù)據(jù)塊的方法。消息隊(duì)列與管道不同的是,消息隊(duì)列是基于消息的,而管道是基于字節(jié)流的。
消息隊(duì)列的創(chuàng)建或取得一個(gè)已存在的消息隊(duì)列:
     int msgget(key_t key,int msgflag);
其中的參數(shù):
        key: 由ftok函數(shù)生成;
        msgflag:
        IPC_CREAT:如果ipc不存在,則創(chuàng)建一個(gè)ipc資源,否則直接打開
        IPC_EXCL:本身沒有太大的意義,只有和 IPC_CREAT一起使用,可以用來保證所得的對(duì)象是新建的,而不是打開已有的對(duì)象.


    下面是一個(gè)用消息隊(duì)列實(shí)現(xiàn)的簡(jiǎn)單的服務(wù)端,客戶端簡(jiǎn)單的收發(fā)消息的程序,具體看看他們的用法。
client.c文件:
#include "comm.h"
   int main()
   {
       int msg_id=get_msg_queue();
       if(msg_id<0)
       {
           exit(1);
       }
      char buf[_BLOCK_SIZE_];
      while(1)
      {
          fflush(stdout);
          printf("please input: ");                                                                                                                                  
          memset(buf,'\0',sizeof(buf));
          gets(buf);
          if(msg_queue_send(msg_id,buf,_CLIENT_ID_)<0)
          {
              printf("sned fail\n");
              exit(1);
          }
          memset(buf,'\0',sizeof(buf));
          if(msg_queue_recv(msg_id,_SERVER_ID_,buf)<0)
          {
              printf("recv fail\n");
              exit(1);
          }
          else
          {
              if(strcmp(buf,"quie")==0)
              {
                printf("server quit\n");
                break;                                                                                                                            
              }
              printf("server: %s\n",buf);
          }
      }
  return 0;
}                                                                                
server.c:文件:
   #include "comm.h"
  
   int main()
   {
       int queue_id=creat_msg_queue();
       if(queue_id<0)
       {
           exit(1);
       }
      char buf[_BLOCK_SIZE_];
      while(1)
      {
          memset(buf,'\0',sizeof(buf));
          if(msg_queue_recv(queue_id,_CLIENT_ID_,buf)<0)
          {
              printf("recv fail\n");
              exit(1);
          }
          else
          {
              if(strcmp(buf,"quit")==0)
              {
                  printf("client quit\n");
                  break;
              }
              printf("client: %s\n",buf);
          }
          printf("please input: ");
          fflush(stdout);
          memset(buf,'\0',sizeof(buf));
          gets(buf);
          if(msg_queue_send(queue_id,buf,_SERVER_ID_)<0)
          {
              printf("send fail\n");
              exit(1);
          }
       }
      destroy_msg_queue(queue_id);
  return 0;
  }
                                                                                                                                                         
comm.h文件:
   #pragma once
  
   #include <stdio.h>
   #include <stdlib.h>
   #include <sys/types.h>
   #include <sys/ipc.h>
   #include <sys/msg.h>
   #include <string.h>
   #include <unistd.h>
  #define _PATH_ "."
  #define _PROJ_ID_ 0x5666                                                                                                                                
  #define _BLOCK_SIZE_ 1024
  #define _SERVER_ID_ 1
  #define _CLIENT_ID_ 2
  struct msgbuf
  {
      long mtype;
      char mtext[_BLOCK_SIZE_];
  };
 
  int comm_msg_queue(int flag);
  int creat_msg_queue();
  int msg_queue_recv(int msg_id,int recv_type,char buf[]);
  int destroy_msg_queue(int msg_id);
  int msg_queue_send(int msg_id,const char* message,long type);
  int get_msg_queue();
                                                                                                                                                           
   comm.c文件:                                                                             
   #include "comm.h"
  
   int comm_msg_queue(int flag)
   {
       key_t _key=ftok(_PATH_,_PROJ_ID_);
       if(_key<0)
       {
           perror("ftok");
           return -1;
      }
      int msg_id=msgget(_key,flag);
      if(msg_id<0)
      {
          perror("msgget");
          return -1;
      }
      return msg_id;
  }
 
  int creat_msg_queue()
  {
      umask(0);
      return comm_msg_queue(IPC_CREAT |IPC_EXCL |0666);
  }
 
  int get_msg_queue()
  {
      return comm_msg_queue(IPC_CREAT);
  }
 
  int msg_queue_send(int msg_id,const char* message,long type)
  {
      struct msgbuf msg;                                                                                                                                             
      msg.mtype=type;
      strcpy(msg.mtext,message);
      if(msgsnd(msg_id,&msg,sizeof(msg.mtext),0)<0)
      {
          perror("msgsnd");
          return -1;
      }                                                                                                                                                              
      return 0;
  }
 
  int msg_queue_recv(int msg_id,int recv_type,char buf[])
  {
      struct msgbuf msg;
      if(msgrcv(msg_id,&msg,sizeof(msg.mtext),recv_type,0)<0)
      {
          perror("msgrcv");
          return -1;
      }
      strcpy(buf,msg.mtext);
      return 0;
  }
 
  int destroy_msg_queue(int msg_id)
  {
      if(msgctl(msg_id,IPC_RMID,NULL)<0)
      {
         perror("msgct");
         return -1;
      }
      else
      {
          printf("remove success\n");
      }
      return 0;
  }
Makefile文件的編寫:

   .PHONY:all
   all:client server
   client:client.c comm.c                                                                                                                                  
       gcc -o $@ $^
   server:server.c comm.c
       gcc -o $@ $^
   .PHONY:clean
   clean:
       rm -f client server

上面程序的運(yùn)行結(jié)果:
            基于消息隊(duì)列的雙向通信


                                                                                                                                                           
     從而實(shí)現(xiàn)了簡(jiǎn)單的收發(fā)消息的功能。


                                                 


















































向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI