基于c语言和消息队列的通信方式实现一个服务器/客户端模式系统:

//服务器.c
#include "myhead.h"


struct msgbuf
{
	long mtype; //消息类型,用来区分不同类型的消息
	long num[3];
};

int main()
{

	int msgid;
	int n;
	//申请消息队列
	name_pipe_init("/home/gec/myfifo");
	msgid=msgget(44555,IPC_CREAT|IPC_EXCL|0777);
	if(msgid==-1)
	{
		if(errno==EEXIST) //已经存在
			msgid=msgget(44555,0777);
		else //别的错误
		{
			perror("申请消息队列失败!\n");
			return -1;
		}
	}
	printf("id号是: %d\n",msgid);
	
	n=getpid();
	write(fd,&n,sizeof(4));
	sleep(5);
    write(fd,&n,sizeof(4));
	//定义结构体变量保存要发送的信息
	struct msgbuf buf1;
	struct msgbuf buf2;
	while(1)
	{
		n=msgrcv(msgid,&buf1,sizeof(buf1.num),getpid(),0); //我指定要接收类型是99的信息
		if(n==-1)
		{
			break;
		}
		printf("pid:%ld申请加法运算\n",buf1.num[2]);
		buf2.mtype=buf1.num[2];
		buf2.num[1]=buf1.num[0]+buf1.num[1];
		buf2.num[0]=0;
		buf2.num[2]=0;
		n=msgsnd(msgid,&buf2,sizeof(buf1.num),0);
	

	}

	
	msgctl(msgid,IPC_RMID,NULL);
	close(fd);
	return 0;
	
}
 
//客户端1.c
#include "myhead.h"

struct msgbuf
{
	long mtype; //消息类型,用来区分不同类型的消息
	long num[3];
};

int main()
{
	int msgid;
	//申请消息队列
	name_pipe_init("/home/gec/myfifo");
	msgid=msgget(44555,IPC_CREAT|IPC_EXCL|0777);
	if(msgid==-1)
	{
		if(errno==EEXIST) //已经存在
			msgid=msgget(44555,0777);
		else //别的错误
		{
			perror("申请消息队列失败!\n");
			return -1;
		}
	}
	printf("id号是: %d\n",msgid);

	struct msgbuf buf1,buf2;
	buf1.mtype=0;
	read(fd,&buf1.mtype,sizeof(buf1.mtype));
	buf1.num[2]=getpid();
	
	while(1)
	{	
		printf("pid:%d\n",gepid());
		printf("输入0都视为退出\n");
		printf("请输入你要的数字1 \n");
		scanf("%ld",&buf1.num[0]);
		printf("请输入你要的数字2 \n");
		scanf("%ld",&buf1.num[1]);
		msgsnd(msgid,&buf1,sizeof(buf1.num),0);
		msgrcv(msgid,&buf2,sizeof(buf1.num),getpid(),0);
		printf("%ld+%ld=%ld\n",buf1.num[0],buf1.num[1],buf2.num[1]);
		if(buf1.num[0]==0&&buf1.num[1]==0)
		{
			break;
		}
	}

	
	msgctl(msgid,IPC_RMID,NULL);
	close(fd);
	return 0;
	
}
 

//客户端2.c
#include "myhead.h"

struct msgbuf
{
	long mtype; //消息类型,用来区分不同类型的消息
	long num[3];
};

int main()
{
	int msgid;
	//申请消息队列
	name_pipe_init("/home/gec/myfifo");
	msgid=msgget(44555,IPC_CREAT|IPC_EXCL|0777);
	if(msgid==-1)
	{
		if(errno==EEXIST) //已经存在
			msgid=msgget(44555,0777);
		else //别的错误
		{
			perror("申请消息队列失败!\n");
			return -1;
		}
	}
	printf("id号是: %d\n",msgid);

	struct msgbuf buf1,buf2;
	buf1.mtype=0;
	read(fd,&buf1.mtype,sizeof(buf1.mtype));
	buf1.num[2]=getpid();
	
	while(1)
	{	
		printf("pid:%d\n",gepid());
		printf("输入0都视为退出\n");
		printf("请输入你要的数字1 \n");
		scanf("%ld",&buf1.num[0]);
		printf("请输入你要的数字2 \n");
		scanf("%ld",&buf1.num[1]);
		msgsnd(msgid,&buf1,sizeof(buf1.num),0);
		msgrcv(msgid,&buf2,sizeof(buf1.num),getpid(),0);
		printf("%ld+%ld=%ld\n",buf1.num[0],buf1.num[1],buf2.num[1]);
		if(buf1.num[0]==0&&buf1.num[1]==0)
		{
			break;
		}
	}

	
	msgctl(msgid,IPC_RMID,NULL);
	close(fd);
	return 0;
	
}
 //从.c
#include "myhead.h"
 


int name_pipe_init(char *mkfifo_path)
{
	int ret;
	//判断有名管道是否存在
	if(access(mkfifo_path,F_OK)!=0) //文件不存在
	{
		//创建有名管道
		ret=mkfifo(mkfifo_path,0777);
		if(ret==-1)
		{
			perror("创建有名管道失败了!\n");
			return -1;
		}
	}
	//打开刚才新建的有名管道
	fd=open(mkfifo_path,O_RDWR);
	if(fd==-1)
	{
		perror("打开管道失败了!\n");
		return -1;
	}
	
}


int close_pipe()
{

    close(fd);
    return 0;
}

//头文件.c
#ifndef _MYHEAD_H
#define _MYHEAD_H
//把所有常用的头文件全部包含
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>  //跟perror有关
#include <linux/input.h> //跟输入子系统模型有关
#include <sys/wait.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/msg.h>

int fd;

int close_pipe();
int name_pipe_init(char *mkfifo_path);

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值