我是靠谱客的博主 兴奋银耳汤,最近开发中收集的这篇文章主要介绍【实验九】Linux的命名管道编程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

一、问题

二、代码

1、serMain.cpp

2、server.h

3、server.cpp

4、cliMain.cpp

5、client.h

6、client.cpp 

7、message.h

8、message.cpp

9.filo.mk

三、运行结果


一、问题

客户端输入数字运算,服务端计算返回结果给客服端,客户端显示结果。

函数参考博客

匿名管道和命名管道的区别:
(1)匿名管道由pipe函数创建并打开。
(2)命名管道由mkfifo函数创建,打开用open
(3)FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的语义。 

二、代码

1、serMain.cpp

#include<unistd.h>
#include"server.h"
int main(int argc,char*argv[]){
	unlink(argv[1]);
	server(argv[1],argv[2]);
	return 0;
}

2、server.h

void server(const char*ser,const char*cli);

3、server.cpp

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include"server.h"
#include"message.h"

void handle_mes(Message& m){
	switch(m.op){
		case 0:
			m.res=m.oprand1+m.oprand2;
			break;
		case 1:
			m.res=m.oprand1-m.oprand2;
			break;
		case 2:
			m.res=m.oprand1*m.oprand2;
			break;
		case 3: 
			if(m.oprand2!=0){
				m.res=m.oprand1/m.oprand2;
			}else{
				m.status='F';
			}
			break;
		default:
			m.status='N';
			break;
	}
}
void process(const int rfd,const int wfd){
	while(true){
		Message m;
		memset(&m,0,sizeof(m));
		int len=read(rfd,&m,sizeof(m));
		if(len<= 0){
			std::cerr<<"client closen";
			break;
		}
		handle_mes(m);
		write(wfd,&m,sizeof(m));
		
	}
}
void server(const char*ser,const char*cli){
	int rfd,wfd;
	do{
		if(mkfifo(ser,0664)==-1){
			std::cerr<<"mkfifo wrongn";
			break;
		}
		rfd=open(ser,O_RDONLY);
		if(rfd == -1){
			std::cerr<<"open wrongn";
			break;
		}
		wfd=open(cli,O_WRONLY);
		if(wfd == -1){
			std::cerr<<"open wrongn";
			break;
		}

		process(rfd,wfd);
	}while(0);
	close(rfd);
    close(wfd);
}

4、cliMain.cpp

#include<unistd.h>
#include"client.h"
int main(int argc,char*argv[]){
	unlink(argv[2]);
	client(argv[1],argv[2]);
	return 0;
}

5、client.h

void client(const char*ser,const char*cli);

6、client.cpp 

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include"client.h"
#include"message.h"
void process(const int rfd ,const int wfd){
	while(true){
		Message m;
		memset(&m,0,sizeof(m));
		std::cin>>m;
		if(std::cin.eof()) break;
		m.status='S';
		write(wfd,&m,sizeof(m));
		read(rfd,&m,sizeof(m));
		std::cout<<m<<std::endl;
	}
}
void client(const char* ser,const char*cli){
	int wfd,rfd;
	do{
		if(mkfifo(cli,0664) == -1){
			std::cerr<<"mkfio wrongn";
			break;
		
		}
		wfd=open(ser,O_WRONLY);
		if(wfd == -1){
		      	std::cerr<<"open wrongn";
			break;
		}
		rfd=open(cli,O_RDONLY);
		if(rfd == -1){
		      	std::cerr<<"open wrongn";
			break;
		}
		process(rfd,wfd);
	}while(0);
	close(wfd);
    close(rfd);
}

7、message.h

#include<iostream>
struct Message{
	int oprand1;
	int op;
	int oprand2;
	int res;
	char status;
};

std::istream& operator >>(std::istream& in,Message& m);
std::ostream& operator <<(std::ostream& out,const Message& m);

8、message.cpp

#include"message.h"

std::istream& operator >>(std::istream& in,Message& m) {
	in>>m.oprand1>>m.op>>m.oprand2;
	return in;
}

std::ostream& operator <<(std::ostream& out,const Message& m){
	if(m.status=='S'){
	out<<m.oprand1<<" "<<m.op<<" "<<m.oprand2<<"="<<m.res;
	}else if(m.status=='N'){
		out<<"operator input error";
	}else if(m.status=='F'){
		out<<"divior is 0";
	}
	return out;
}

9.filo.mk

GCC=g++
CFLAG=-c
OFLAG=-o
SEXE=serMain
SOBJ=serMain.o server.o
CEXE=cliMain
COBJ=cliMain.o client.o message.o
all:${SEXE} ${CEXE}
${SEXE}:${SOBJ}
        ${GCC} ${OFLAG} $@ $^
${CEXE}:${COBJ}
        ${GCC} ${OFLAG} $@ $^
%.o:%.cpp
        ${GCC} ${CFLAG} $^
clean:    
        rm *.o

 信息由加减乘除登录账号和密码,只需修改以下几个地方。

server.cpp

const char ifname[]="login.txt";
void han_mes(Message& m){
	std::ifstream fin(ifname);
	std::string user;
	std::string password;
        while(fin>>user>>password){
	if(user==m.user&&password==m.password){
		m.status='S';
	}else{
		m.status='N';
	}
	}
	fin.close();
	
}

message.h 

#include<iostream>
#include<string.h>
struct Message{

	char user[10];
	char password[10];
	char status;
};

std::istream &operator >>(std::istream& in, Message& m);
std::ostream &operator <<(std::ostream& out,const Message& m);

message.cpp 

#include"message.h"

std::istream& operator >>(std::istream& in,Message& m) {
	in>>m.user>>m.password;
	return in;
}

std::ostream& operator <<(std::ostream& out,const Message& m){
	if(m.status=='S'){
	out<<"login sucessful";
	}else if(m.status=='N'){
		out<<"login error";
	}	
	return out;
}

三、运行结果

最后

以上就是兴奋银耳汤为你收集整理的【实验九】Linux的命名管道编程的全部内容,希望文章能够帮你解决【实验九】Linux的命名管道编程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部