十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
消息隊(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)的簡單的服務(wù)端,客戶端簡單的收發(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
#include
#include
#include
#include
#include
#include
#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é)果:
成都創(chuàng)新互聯(lián)總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、小程序開發(fā)、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
從而實(shí)現(xiàn)了簡單的收發(fā)消息的功能。