先放下代码  回来在解释

头文件:

clientinfo.h

 struct CLIENTINFO{
char myfifo[];
int leftarg;
int rightarg;
char op;
};
typedef struct CLIENTINFO CLIENTINFO, *CLINTINFOPTR;

client.c

 #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include "clientinfo.h" #define FIFO_NAME "/home/levi/chatapplication/data/server_fifo/chat_server_fifo"
#define BUFF_SZ 100 char mypipename[BUFF_SZ];
void handler(int sig){
unlink(mypipename);
exit();
} int main(int argc, char const *argv[])
{
int res;
int fifo_fd, my_fifo;
int fd;
CLIENTINFO info;
char buffer[BUFF_SZ]; signal(SIGKILL, handler);
signal(SIGINT, handler);
signal(SIGTERM, handler); if(argc != ){
printf("Usage: %s op1 operation op2\n", argv[]);
exit();
}
if(access(FIFO_NAME, F_OK) == -){
printf("Could not open FIFO %s\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
fifo_fd = open(FIFO_NAME, O_WRONLY);
if(fifo_fd == -){
printf("Could not open %s for write access\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
sprintf(mypipename, "/home/levi/chat_client_%d_fifo", getpid());
res = mkfifo(mypipename, );
if(res != ){
printf("FIFO %s was not created\n", buffer);
exit(EXIT_FAILURE);
} my_fifo = open(mypipename, O_RDONLY | O_NONBLOCK);
if(my_fifo == -){
printf("Could not open %s for read only access\n", FIFO_NAME);
exit(EXIT_FAILURE);
} strcpy(info.myfifo, mypipename);
info.leftarg = atoi(argv[]);
info.op = argv[][];
info.rightarg = atoi(argv[]); write(fifo_fd, &info, sizeof(CLIENTINFO));
close(fifo_fd); memset(buffer, '\0', BUFF_SZ);
while(){
res = read(my_fifo, buffer, BUFF_SZ);
if(res > ){
printf("Received from server : %s\n", buffer);
break;
}
}
printf("Client %d is terminating\n", getpid());
close(my_fifo);
(void)unlink(mypipename);
return ;
}

server.c

 #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include "clientinfo.h" #define FIFO_NAME "/home/levi/server_fifo/chat_server_fifo"
void handler(int sig){
unlink(FIFO_NAME);
exit();
} int calc(CLIENTINFOPTR info){
switch(info->op){
case '+' : return info->leftarg + info->rightarg;
case '-' : return info->leftarg - info->rightarg;
case '*' : return info->leftarg * info->rightarg;
case '/' : return info->leftarg / info->rightarg;
}
return ;
} int main(int argc, char const *argv[])
{
int res, i, fifo_fd, fd1;
CLIENTINFO info;
char buffer[];
signal(SIGKILL, handler);
signal(SIGINT, handler);
signal(SIGTERM, handler);
if(access(FIFO_NAME, F_OK) == -){
res = mkfifo(FIFO_NAME, );
if(res != ){
printf("FIFO %s was not created\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
fifo_fd = open(FIFO_NAME, O_RDONLY);
if(fifo_fd == -){
printf("Could not open %s for read only access\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
printf("\nServer is rarin to go !\n");
while(){
res = read(fifo_fd, &info, sizeof(CLIENTINFO));
if(res != ){
printf("Client arrived!!\n");
sprintf(buffer, "The result is %d", calc(&info));
fd1 = open(info.myfifo, O_WRONLY | O_NONBLOCK);
write (fd1, buffer, strlen(buffer) + );
close(fd1);
} }
return ;
}

OK!

./server.c &

./client 3 + 5

  看输出吧!

Linux系统编程:客户端-服务器用FIFO进行通信的更多相关文章

  1. Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道

    Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道 背景 上一讲我们介绍了创建子进程的方式.我们都知道,创建子进程是为了与父进程协作(或者是为了执行新的程序,参考 Linux ...

  2. Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号

    Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...

  3. Linux 系统编程 学习:04-进程间通信2:System V IPC(1)

    Linux 系统编程 学习:04-进程间通信2:System V IPC(1) 背景 上一讲 进程间通信:Unix IPC-信号中,我们介绍了Unix IPC中有关信号的概念,以及如何使用. IPC的 ...

  4. Linux 系统编程 学习:05-进程间通信2:System V IPC(2)

    Linux 系统编程 学习:05-进程间通信2:System V IPC(2) 背景 上一讲 进程间通信:System V IPC(1)中,我们介绍了System IPC中有关消息队列.共享内存的概念 ...

  5. Linux 系统编程 学习 总结

    背景 整理了Liunx 关于 进程间通信的 很常见的知识. 目录 与 说明 Linux 系统编程 学习:000-有关概念 介绍了有关的基础概念,为以后的学习打下基础. Linux 系统编程 学习:00 ...

  6. Linux系统编程@进程通信(一)

    进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...

  7. Linux系统编程温故知新系列 --- 01

    1.大端法与小端法 大端法:按照从最高有效字节到最低有效字节的顺序存储,称为大端法 小端法:按照从最低有效字节到最高有效字节的顺序存储,称为小端法 网际协议使用大端字节序来传送TCP分节中的多字节整数 ...

  8. Linux 系统编程 学习:06-基于socket的网络编程1:有关概念

    Linux 系统编程 学习:006-基于socket的网络编程1:有关概念 背景 上一讲 进程间通信:System V IPC(2)中,我们介绍了System IPC中关于信号量的概念,以及如何使用. ...

  9. Linux 系统编程 学习:07-基于socket的网络编程2:基于 UDP 的通信

    Linux 系统编程 学习:07-基于socket的网络编程2:基于 UDP 的通信 背景 上一讲我们介绍了网络编程的一些概念.socket的网络编程的有关概念 这一讲我们来看UDP 通信. 知识 U ...

随机推荐

  1. contentWindow,

    a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象.b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...

  2. [cocos2dx注意事项009]试用quick-cocos2dx-2.2.4

    quick-cocos2d-x 在中国站http://quick.cocoachina.com/?page_id=40,用他们自己的话说:quick-cocos2d-x(后文简称 quick)与 co ...

  3. Access Toke调用受保护的API

    ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API 在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端 ...

  4. CUMCM--总结

    有些事情经历一次就好,一次的经历足以成长. 其实,对于数模真的没什么要说的,也没什么好写的.从9月11日,8点见到赛题,到今天早上8点的提交,短短的三天,度过寂静的黑夜,见到微曦的黎明.三天三夜,9个 ...

  5. Unobtrusive JavaScript 是什么?

    Unobtrusive JavaScript 是什么? <!--以下是常规Javascript下写出来的Ajax--><div id="test">< ...

  6. iWatch # 初始化工程

    iWatch --利用swift,开发iWatch手表小应用! 远程仓库,团队开发: $ git init $ git add . $ git commit -m “ProjectName” // p ...

  7. SVN & Git (一)

    (一)SVN的使用.CornerStone图形化管理工具! SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. ...

  8. Oracle推断领域包括中国

    假设你要推断领域包括中国.有一个简单的方法. SQL> drop table test purge; SQL> create table test as select * from dba ...

  9. 让Docker功能更强大的10个开源工具

    让Docker功能更强大的10个开源工具 更好的管理.Web前端程序.更深入地了解容器应用程序,Docker生态系统正在迅速发展,这还得归功于其充满活力的开源社区. 软件项目的成功常常根据其催生的生态 ...

  10. 用python 爬取网页图片

    import re import string import sys import os import urllib url="http://tieba.baidu.com/p/252129 ...