两个有名管道实现qq通信
来源:互联网 发布:女鬼墨捏脸数据 编辑:程序博客网 时间:2024/06/01 09:17
1.先创建两个有名管道:
/* ============================================================================ Name : qqConnext.c Author : Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>int main(int argc,char **argv){if(mkfifo("rdfifo",0666)<0){perror("create pipe wrong!");exit(1);}if(mkfifo("wrfifo",0666)<0){perror("create pipe wrong!");exit(1);}return EXIT_SUCCESS;}
2.client端:一共两个进程,一个用来读,一个用来写
#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<string.h>#include<unistd.h>int main(){pid_t pid;int rfd,wfd;char buf[1024];int len;umask(0);while((rfd=open("wrfifo",O_RDONLY))==-1);wfd=open("rdfifo",O_RDWR);if(wfd==-1){perror("client write wrong");}pid=fork();if(pid==0){while(1){printf("client:");fgets(buf,1024,stdin);buf[strlen(buf)-1]='\0'; if(strcmp(buf,"quit")==0){close(rfd);unlink("rdfifo"); exit(0);}write(wfd,buf,strlen(buf));}}else{while(1){len=read(rfd,buf,1024);if(len==-1){perror("client read wrong");}else{ buf[len]='\0';printf("server:%s\n",buf);}}}exit(0);}
3.server端:
#include <stdio.h>#include <stdlib.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<string.h>#include<unistd.h>int main(int argc,char **argv){ pid_t pid; int wfd,rfd; char buf[1024]; int len; wfd=open("wrfifo",O_RDWR); umask(0); if(wfd==-1) { perror("open pipe wrfifo wrong!"); exit(1); } while((rfd=open("rdfifo",O_RDONLY))==-1) { } pid=fork(); if(pid==0) { while(1) { len=read(rfd,buf,1024); if(len==-1) { perror("server write wrong"); exit(1); } else { buf[len]='\0'; printf("Client:%s\n",buf); } } } else if(pid>0) { while(1) { printf("server:"); fgets(buf,1024,stdin);//从标准输入获取 buf[strlen(buf)-1]='\0'; if(strcmp(buf,"q")==0) { close(wfd); unlink("wrfifo"); exit(0); } write(wfd,buf,strlen(buf)); } } exit(0);}
- 两个有名管道实现qq通信
- 两个有名管道实现qq通信 续
- 有名管道实现进程间通信。
- Linux下的有名管道(05)---使用两个管道实现两个进程之间的通信(对讲机模式)
- Linux下的有名管道(06)---使用两个管道实现两个进程之间的通信(手机模式)
- linux有名管道通信
- 进程通信--有名管道
- 有名管道的通信
- 有名管道通信
- 利用有名管道,实现两个独立进程间的通讯
- 通过有名管道实现两个进程单向通讯
- 利用两个有名管道实现类似聊天功能
- 1.利用有名管道和多线程实现双方互相通信
- Linux进程间通信---有名管道 代码实现
- linux中利用有名管道实现进程之间的通信
- Linux进程通信 有名管道实现守护进程
- 有名管道的实现
- [IPC]通信有名管道FIFO
- MySQL学习之数据类型
- 自己动手定制一个高效阅读源代码的vim
- is小实例
- 卷积的物理意义
- Android那些事儿之自定义进度条
- 两个有名管道实现qq通信
- JS 学习大全
- 微软面试100题-4.在二元树中找出和为某一值的所有路径
- No package 'gl' found
- 接口约定参数
- 网络信息安全对抗(北京邮电大学版)考点总结
- php框架codeigniter中如何使用框架的session
- CListCtrl的基本使用
- 图像卷积与模板