有名管道的应用
来源:互联网 发布:excel数据有效性英文 编辑:程序博客网 时间:2024/04/29 10:51
通过创建两个管道可以实现进程间的全双工通信,同样也可以通过创建两个FIFO来实现不同进程间的全双工通信。
Server端#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#define FIFO_READ "readfifo"#define FIFO_WRITE "writefifo"#define BUF_SIZE 1024int main(int argc, char *argv[]){ int wfd, rfd; char buf[BUF_SIZE]; int len; umask(0); if (mkfifo(FIFO_WRITE, S_IFIFO | 0666)) { printf("Can't create FIFO %s because %s",FIFO_WRITE,strerror(errno)); exit(1); } umask(0); wfd = open(FIFO_WRITE,O_WRONLY); if (wfd == -1) { printf("open FIFO %s error: %s",FIFO_WRITE,strerror(errno)); exit(1); } while ((rfd = open(FIFO_READ,O_RDONLY)) == -1) { sleep(1); } while (1) { printf("Server: "); fgets(buf, BUF_SIZE, stdin); buf[strlen(buf)-1] = '\0'; if (strncmp(buf, "quit", 4) == 0) { close(wfd); unlink(FIFO_WRITE); close(rfd); exit(0); } write(wfd,buf,strlen(buf)); len = read(rfd, buf, BUF_SIZE); if (len > 0) { buf[len] = '\0'; printf("Client: %s\n",buf); } } return 0;}
Client端#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <unistd.h>#define FIFO_READ "writefifo"#define FIFO_WRITE "readfifo"#define BUF_SIZE 1024int main(int argc, char *argv[]){ int wfd, rfd; char buf[BUF_SIZE]; int len; umask(0); if (mkfifo(FIFO_WRITE, S_IFIFO | 0666)) { printf("Can't create FIFO %s because %s",FIFO_WRITE,strerror(errno)); exit(1); } while ((rfd = open(FIFO_READ, O_RDONLY)) == -1) { sleep(1); } wfd = open(FIFO_WRITE, O_WRONLY); if (wfd == -1) { printf("Fail to open FIFO %s : %s",FIFO_WRITE,strerror(errno)); exit(-1); } while (1) { len = read(rfd, buf, BUF_SIZE); if (len > 0) { buf[len] = '\0'; printf("Server: %s\n",buf); } printf("Client: "); fgets(buf, BUF_SIZE, stdin); buf[strlen(buf)-1] = '\0'; if (strncmp(buf, "quit", 4) == 0) { close(wfd); unlink(FIFO_WRITE); close(rfd); exit(0); } write(wfd, buf, strlen(buf)); } return 0;}
1 0
- 有名管道的应用
- 有名管道的用法
- 有名管道的创建
- 有名管道的用法
- 有名管道的实现
- 有名管道的使用
- 有名管道的通信
- 有名管道的打开规则
- 有名管道的读写程序
- 有名管道FIFO的读写
- FIFO 有名管道的使用
- linux的有名管道学习
- 有名管道FIFO的使用
- 有名管道的写操作
- 有名管道和无名管道的区别
- 有名管道和无名管道的区别
- 有名管道和无名管道的区别
- linux应用开发-有名管道编程
- 进程间通信1
- 关于插入排序的逻辑错误和思想
- HDU5927 Auxiliary Set ->树形DP
- 获取QTableWidget中setCellWidget中控件所在的行列信息
- 让XNA显示中文
- 有名管道的应用
- hdu 5927 Auxiliary Set (分析*)
- 设计模式(四)单例模式
- linux编程获取本机IP地址
- 欢迎使用CSDN-markdown编辑器
- 实验吧之CTF---紧急报文
- Altium Designer Summer 09 将PCB图输出装配图的方法
- 51Nod-1714-B君的游戏
- 简单组合java.util.Map<K,V>实现Map<K,P,V>