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 ...
随机推荐
- c语言获取符号位整数和浮点
1. 为什么你应该得到的签位 非常多的时间,我们需要推断的数目值正和负,做了相应的逻辑处理.完成这一要求条件推断语句可以很好. 有时会出现以下情况, if (x > 0) { x = x - 1 ...
- SSH框架总结(帧分析+环境结构+示例源代码下载)
首先,SSH不是一个框架.而是多个框架(struts+spring+hibernate)的集成,是眼下较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- document.domain跨域
原文:[转载]document.domain跨域 document.domain 用来得到当前网页的域名. 比如在地址栏里输入: javascript:alert(document.domain); ...
- 什么时候rootViewController至tabbarController时刻,控制屏幕旋转法
于ios6后,ios系统改变了屏幕旋转的方法,假设您想将屏幕旋转法,在需求rootvc里面制备,例如 UIViewController *viewCtrl = [[UIViewController a ...
- TableLayout中stretchColumns、shrinkColumns的使用方法
android:stretchColumns="1" android:shrinkColumns="1"这两个属性是TableLayout所特有的,也是这两个属 ...
- android 登录推断是否邮箱或手机号
/** * 验证邮件是合法的输入 * * @param strEmail * @return */ public static boolean isEmail(String strEmail) { ...
- js之with
网上找资料的时候看到有js代码里出现了大量的with语句,有点好奇,这里做下总结: 1)简要说明 with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性. ...
- jquery扩展
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- Introducing Visual Studio’s Emulator for Android
visual studio 2015支持Android开发了. Microsoft released Visual Studio 2015 Preview this week and with it ...
- Hibernat之关系的处理一对一处理
第一步:编写两个pojo,比如一个学生表一个班级表 这里使用注解. 需要 公司表: package com.qcf.pox; import javax.persistence.CascadeType ...