Linux系统编程:客户端-服务器用FIFO进行通信
先放下代码 回来在解释
头文件:
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进行通信的更多相关文章
- Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道
Linux 系统编程 学习:02-进程间通信1:Unix IPC(1)管道 背景 上一讲我们介绍了创建子进程的方式.我们都知道,创建子进程是为了与父进程协作(或者是为了执行新的程序,参考 Linux ...
- Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号
Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...
- Linux 系统编程 学习:04-进程间通信2:System V IPC(1)
Linux 系统编程 学习:04-进程间通信2:System V IPC(1) 背景 上一讲 进程间通信:Unix IPC-信号中,我们介绍了Unix IPC中有关信号的概念,以及如何使用. IPC的 ...
- Linux 系统编程 学习:05-进程间通信2:System V IPC(2)
Linux 系统编程 学习:05-进程间通信2:System V IPC(2) 背景 上一讲 进程间通信:System V IPC(1)中,我们介绍了System IPC中有关消息队列.共享内存的概念 ...
- Linux 系统编程 学习 总结
背景 整理了Liunx 关于 进程间通信的 很常见的知识. 目录 与 说明 Linux 系统编程 学习:000-有关概念 介绍了有关的基础概念,为以后的学习打下基础. Linux 系统编程 学习:00 ...
- Linux系统编程@进程通信(一)
进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...
- Linux系统编程温故知新系列 --- 01
1.大端法与小端法 大端法:按照从最高有效字节到最低有效字节的顺序存储,称为大端法 小端法:按照从最低有效字节到最高有效字节的顺序存储,称为小端法 网际协议使用大端字节序来传送TCP分节中的多字节整数 ...
- Linux 系统编程 学习:06-基于socket的网络编程1:有关概念
Linux 系统编程 学习:006-基于socket的网络编程1:有关概念 背景 上一讲 进程间通信:System V IPC(2)中,我们介绍了System IPC中关于信号量的概念,以及如何使用. ...
- Linux 系统编程 学习:07-基于socket的网络编程2:基于 UDP 的通信
Linux 系统编程 学习:07-基于socket的网络编程2:基于 UDP 的通信 背景 上一讲我们介绍了网络编程的一些概念.socket的网络编程的有关概念 这一讲我们来看UDP 通信. 知识 U ...
随机推荐
- 产品 线上 保持 和 支持 服务 (Support and maintenance solutions)
Maintenance and support are the key factors for the smooth functioning of ERP solutions. ERP mainten ...
- QtQuick桌面应用程序开发指导 3)达到UI而功能_B 4)动态管理Note物_A
3.2 把Page Item和Marker Item绑定 之前我们实现了PagePanel组件, 使用了三个state来切换Page组件的opacity属性; 这一步我们会使用Marker和Marke ...
- 开展project
正常的生活之路
相对刚走出学校的学生在其他行业工作,竞争力的薪酬,同时.并不断地不仅学习更新专业知识让你感到生活的充实,更满足了你那不让外人知的虚荣心.在刚出校门的几年中,你常常回头看看被你落在后面的同学们,在内心怜 ...
- 第11章 享元模式(Flyweight Pattern)
原文 第11章 享元模式(Flyweight Pattern) 概述: 面向对象的思想很好地解决了抽象性的问题,一般也不会出现性能上的问题.但是在某些情况下,对象的数量可能会太多,从而导致了运行时 ...
- 读取xml文件"分析 EntityName 时出错"的解决方案
在涉及到xml与xslt编程的过程中,经常会碰到"分析 EntityName 时出错"的提示,这个不是程序错误,是因为xml文件中使用了一些特殊符号导致的. XML 节点中不 ...
- C#关于HttpClient的应用(一):获取IP所在的地理位置信息
public class IpHttpClient:BaseHttpClient { private String appKey; private const string HOST_PATH = & ...
- MongoDB详解学习历程
MongoDB是一个基于分布式文件存储的数据库,它是介于关系数据库和非关系数据库之间的产品. MongoDB支持的数据结构非常松散,类似json的bjson格式,因此可以存储比较复杂的数据类型.Mon ...
- Cocos2D-X2.2.3学习笔记3(内存管理)
本章节介绍例如以下: 1.C/C++内存管理机制 2.引用计数机制 3.自己主动释放机制 1.C/C++内存管理机制 相信仅仅要懂oop的都知道NEW这个keyword吧,这个通俗点说事实上就是创建对 ...
- IOS开发中绘制地图线路
地图应用经常会涉及到线路的绘制问题,ios下可以使用MKMapView进行地图开发,使用 MKOverlayView进行线路的绘制. 使用MKMapView添加MKMap.framework 和Cor ...
- 项目管理实践 -- 健身小管家(Fitness housekeeper)的管理(2)
继续跟踪进度 按计划应该在14号完成的工作,今天已经完成了.