我是靠谱客的博主 兴奋银耳汤,这篇文章主要介绍【实验九】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

复制代码
1
2
3
4
5
6
7
8
#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

复制代码
1
void server(const char*ser,const char*cli);

3、server.cpp

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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

复制代码
1
2
3
4
5
6
7
#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

复制代码
1
void client(const char*ser,const char*cli);

6、client.cpp 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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

复制代码
1
2
3
4
5
6
7
8
9
10
11
#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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
#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 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部