Linux系统编程:客户端-服务器用FIFO进行通信

先放下代码  回来在解释

头文件:

clientinfo.h

1 struct CLIENTINFO{
2     char myfifo[500];
3     int leftarg;
4     int rightarg;
5     char op;
6 }; 
7 typedef struct CLIENTINFO CLIENTINFO, *CLINTINFOPTR;


client.c

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <sys/types.h>
 6 #include <fcntl.h>
 7 #include <signal.h>
 8 #include "clientinfo.h"
 9 
10 #define FIFO_NAME "/home/levi/chatapplication/data/server_fifo/chat_server_fifo"
11 #define BUFF_SZ 100
12 
13 char mypipename[BUFF_SZ];
14 void handler(int sig){
15     unlink(mypipename);
16     exit(1);
17 }
18 
19 int main(int argc, char const *argv[])
20 {
21     int res;
22     int fifo_fd, my_fifo;
23     int fd;
24     CLIENTINFO info;
25     char buffer[BUFF_SZ];
26 
27     signal(SIGKILL, handler);
28     signal(SIGINT, handler);
29     signal(SIGTERM, handler);
30 
31     if(argc != 4){
32         printf("Usage: %s op1 operation op2\n", argv[0]);
33         exit(1);
34     }
35     if(access(FIFO_NAME, F_OK) == -1){
36         printf("Could not open FIFO %s\n", FIFO_NAME);
37         exit(EXIT_FAILURE);
38     }
39     fifo_fd = open(FIFO_NAME, O_WRONLY);
40     if(fifo_fd == -1){
41         printf("Could not open %s for write access\n", FIFO_NAME);
42         exit(EXIT_FAILURE);
43     }
44     sprintf(mypipename, "/home/levi/chat_client_%d_fifo", getpid());
45     res = mkfifo(mypipename, 0777);
46     if(res != 0){
47         printf("FIFO %s was not created\n", buffer);
48         exit(EXIT_FAILURE);
49     }
50 
51     my_fifo = open(mypipename, O_RDONLY | O_NONBLOCK);
52     if(my_fifo == -1){
53         printf("Could not open %s for read only access\n", FIFO_NAME);
54         exit(EXIT_FAILURE);
55     }
56 
57     strcpy(info.myfifo, mypipename);
58     info.leftarg = atoi(argv[1]);
59     info.op = argv[2][0];
60     info.rightarg = atoi(argv[3]);
61 
62     write(fifo_fd, &info, sizeof(CLIENTINFO));
63     close(fifo_fd);
64 
65     memset(buffer, '\0', BUFF_SZ);
66     while(1){
67         res = read(my_fifo, buffer, BUFF_SZ);
68         if(res > 0){
69             printf("Received from server : %s\n", buffer);
70             break;
71         }
72     }
73     printf("Client %d is terminating\n", getpid());
74     close(my_fifo);
75     (void)unlink(mypipename);
76     return 0;
77 }

 

server.c

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <sys/types.h>
 6 #include <fcntl.h>
 7 #include <signal.h>
 8 #include "clientinfo.h"
 9 
10 #define FIFO_NAME "/home/levi/server_fifo/chat_server_fifo"
11 void handler(int sig){
12     unlink(FIFO_NAME);
13     exit(1);
14 }
15 
16 int calc(CLIENTINFOPTR info){
17     switch(info->op){
18         case '+' : return info->leftarg + info->rightarg;
19         case '-' : return info->leftarg - info->rightarg;
20         case '*' : return info->leftarg * info->rightarg;
21         case '/' : return info->leftarg / info->rightarg;
22     }
23     return 0;
24 }
25 
26 int main(int argc, char const *argv[])
27 {
28     int res, i, fifo_fd, fd1;
29     CLIENTINFO info;
30     char buffer[100];
31     signal(SIGKILL, handler);
32     signal(SIGINT, handler);
33     signal(SIGTERM, handler);
34     if(access(FIFO_NAME, F_OK) == -1){
35         res = mkfifo(FIFO_NAME, 0777);
36         if(res != 0){
37             printf("FIFO %s was not created\n", FIFO_NAME);
38             exit(EXIT_FAILURE);
39         }
40     }
41     fifo_fd = open(FIFO_NAME, O_RDONLY);
42     if(fifo_fd == -1){
43         printf("Could not open %s for read only access\n", FIFO_NAME);
44         exit(EXIT_FAILURE);
45     }
46     printf("\nServer is rarin to go !\n");
47     while(1){
48         res = read(fifo_fd, &info, sizeof(CLIENTINFO));
49         if(res != 0){
50             printf("Client arrived!!\n");
51             sprintf(buffer, "The result is %d", calc(&info));
52             fd1 = open(info.myfifo, O_WRONLY | O_NONBLOCK);
53             write (fd1, buffer, strlen(buffer) + 1);
54             close(fd1);
55         }
56 
57     }
58     return 0;
59 }


OK!

./server.c &

./client 3 + 5

  看输出吧!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux系统提供了各种系统调用API用于进程之间的通信:    无名管道PIPE    命名管道FIFO    消息队列    共享内存    信号量    文件锁    信号signal....其中还包括system V和POSIX 两种接口标准,除此之外,Linux系统自身还扩展了自己的一套API接口用于进程间通信,比如signalfd、timerfd、eventfd等。本视频教程为《Linux系统编程》第05期,本期课程将会带领大家学习Linux下将近15种进程间通信IPC工具的使用,了解它们的通信机制、编程实例、使用场景、内核中的实现以及各自的优缺点。本课程会提供PDF版本的PPT课件和代码,学员购买课程后可到课程主页自行下载嵌入式自学路线指导图:------------------------------------------------------------------------------------------------------                   《嵌入式工程师自我修养》嵌入式自学系列教程                                          作者:王利涛------------------------------------------------------------------------------------------------------一线嵌入式工程师精心打造,嵌入式学习路线六步走: 第 1 步:Linux三剑客零基础玩转Linux+UbuntuGit零基础实战:Linux开发技能标配vim从入门到精通基础篇:零基础学习vim基本命令vim从入门到精通定制篇:使用插件打造嵌入式开发IDEmakefile工程实践基础篇:从零开始一步一步写项目的Makefilemakefile工程实践第2季:使用Autotools自动生成Makefile软件调试基础理论printf打印技巧Linux内核日志与打印使用QEMU搭建u-boot+Linux+NFS嵌入式开发环境第 2 步:C语言嵌入式Linux高级编程第1期:C语言进阶学习路线指南第2期:计算机架构与ARM汇编程序设计第3期:程序的编译、链接和运行原理第4期:堆栈内存管理第6期:数据存储与指针第7期:嵌入式数据结构与Linux内核的OOP思想第8期:C语言的模块化编程第9期:CPU和操作系统入门      搞内核驱动开发、光会C语言是不行的!      你还需要学习的有很多,包括:计算机体系架构、ARM汇编、程序的编译链接运行原理、CPU和操作系统原理、堆栈内存管理、指针、linux内核中的面向对象思想、嵌入式系统架构、C语言的模块化编程.....第 3 步:Linux系统编程第00期:Linux系统编程入门第01期:揭开文件系统的神秘面纱第02期:文件I/O编程实战第03期:I/O缓存与内存映射第04期:打通进程与终端的任督二脉第05期:进程间通信-------------------we are here!‍    第 4 步:Linux内核编程‍    练乾坤大挪移,会不会九阳神功,是一道坎。搞驱动内核开发,懂不懂内核也是一道坎。第 5 步:嵌入式驱动开发    芯片原理、datasheet、硬件电路、调试手段、总线协议、内核机制、框架流程....第 6 步:项目实战    嵌入式、嵌入式人工智能、物联网、智能家居...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值