linux有名管道实现聊天功能
有名管道实现聊天功能 chatA.c chatB.c
获取键盘录入数据: scanf()函数遇到换行自动结束
持续录入数据:fgets()函数
chatA.c
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <fcntl.h> 8 9 int main() 10 { 11 //1.判断有名管道文件是否存在 12 int ret = access("fifo1",F_OK); 13 if(ret == -1) 14 { 15 //文件不存在 16 printf("管道不存在,创建对应的有名管道fifo1\n"); 17 ret = mkfifo("fifo1",0664); 18 if(ret == -1) 19 { 20 perror("mkfifo"); 21 exit(0); 22 } 23 } 24 ret = access("fifo2",F_OK); 25 if(ret == -1) 26 { 27 //文件不存在 28 printf("管道不存在,创建对应的有名管道fifo2\n"); 29 ret = mkfifo("fifo2",0664); 30 if(ret == -1) 31 { 32 perror("mkfifo"); 33 exit(0); 34 } 35 } 36 //2.以只写的方式打开管道fifo1 37 int fdw = open("fifo1",O_WRONLY); 38 if(fdw == -1) 39 { 40 perror("open"); 41 exit(0); 42 } 43 printf("打开fifo1成功,等待写入数据\n"); 44 //3.以只读的方式打开管道fifo2 45 int fdr = open("fifo2",O_RDONLY); 46 if(fdr == -1) 47 { 48 perror("open"); 49 exit(0); 50 } 51 printf("打开fifo2成功,等待读取...\n"); 52 //4.循环写读数据 53 char buf[128]; 54 while(1) 55 { 56 memset(buf,0,128);//清空buf 57 //获取标准输入的数据 58 fgets(buf,128,stdin);//stdin :file*文件指针 59 //写数据 60 ret = write(fdw,buf, strlen(buf)); 61 if(ret == -1) 62 { 63 perror("write"); 64 exit(0); 65 } 66 //5.读取管道数据 67 memset(buf,0,128); 68 ret = read(fdr,buf,128); 69 if(ret <= 0) 70 { 71 perror("read"); 72 exit(0); 73 } 74 printf("buf: %s\n",buf); 75 } 76 //关闭文件描述符 77 close(fdr); 78 close(fdw); 79 return 0; 80 }
chatB.c
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <fcntl.h> 8 //不知道函数的头文件 就使用 man 2/3 函数 查询 9 int main() 10 { 11 //1.判断有名管道文件是否存在 12 int ret = access("fifo1",F_OK); 13 if(ret == -1) 14 { 15 //文件不存在 16 printf("管道不存在,创建对应的有名管道fifo1\n"); 17 ret = mkfifo("fifo1",0664); 18 if(ret == -1) 19 { 20 perror("mkfifo"); 21 exit(0); 22 } 23 } 24 ret = access("fifo2",F_OK); 25 if(ret == -1) 26 { 27 //文件不存在 28 printf("管道不存在,创建对应的有名管道fifo2\n"); 29 ret = mkfifo("fifo2",0664); 30 if(ret == -1) 31 { 32 perror("mkfifo"); 33 exit(0); 34 } 35 } 36 //2.以只读的方式打开管道fifo1 37 int fdr = open("fifo1",O_RDONLY); 38 if(fdr == -1) 39 { 40 perror("open"); 41 exit(0); 42 } 43 printf("打开fifo1成功,等待读取...\n"); 44 //3.以只写的方式打开管道fifo2 45 int fdw = open("fifo2",O_WRONLY); 46 if(fdw == -1) 47 { 48 perror("open"); 49 exit(0); 50 } 51 printf("打开fifo2成功,等待写入数据\n"); 52 //4.循环读写数据 53 char buf[128]; 54 while(1) 55 { 56 //读取管道数据 57 memset(buf,0,128); 58 ret = read(fdr,buf,128); 59 if(ret <= 0) 60 { 61 perror("read"); 62 exit(0); 63 } 64 printf("buf: %s\n",buf); 65 //写管道数据 66 memset(buf,0,128);//清空buf 67 //获取标准输入的数据 68 fgets(buf,128,stdin); 69 //写数据 70 ret = write(fdw,buf, strlen(buf)); 71 if(ret == -1) 72 { 73 perror("write"); 74 exit(0); 75 } 76 } 77 //关闭文件描述符 78 close(fdr); 79 close(fdw); 80 return 0; 81 }
拓展:此时AB进程只能进行单句发送接收,想要多句发送或接收 可以对AB分别创建子进程 双端父子进程分别进行 读取操作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)