命名管道(FIFO)聊天程序
来源:互联网 发布:linux xargs cat命令 编辑:程序博客网 时间:2024/05/18 03:57
//A.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <error.h>
int main()
{
int i,rfd,wfd,len=0;
char str[32];
int flag,stdinflag;
fd_set read_fd;
struct timeval net_timer;
mkfifo("fifo1",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); //管道只能实现单向通信,需创建两个管道
mkfifo("fifo2",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
wfd=open("fifo1",O_WRONLY);
rfd=open("fifo2",O_RDONLY);
if(rfd<=0||wfd<=0)
return 0;
printf("A\n");
while(1)
{
FD_ZERO(&read_fd);
FD_SET(rfd,&read_fd);
FD_SET(fileno(stdin),&read_fd);
net_timer.tv_sec=5;
net_timer.tv_usec=0;
memset(str,0,sizeof(str));
if(i=select(rfd+1,&read_fd,NULL,NULL,&net_timer)==0) //利用select完成非阻塞监听
continue;
else if(i<0)
exit(1);
if(FD_ISSET(rfd,&read_fd))
{
if(read(rfd,str,sizeof(str))>0)
printf("B:%s",str);
}
if(FD_ISSET(fileno(stdin),&read_fd))
{
fgets(str,sizeof(str),stdin);
len=write(wfd,str,sizeof(str));
}
}
close(rfd);
close(wfd);
}
//B.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <error.h>
int main()
{
int i,rfd,wfd,len=0;
char str[32];
int flag,stdinflag;
fd_set read_fd;
struct timeval net_timer;
mkfifo("fifo1",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
mkfifo("fifo2",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
rfd=open("fifo1",O_RDONLY);
wfd=open("fifo2",O_WRONLY);
if(rfd<=0||wfd<=0)
return 0;
printf("B\n");
while(1)
{
FD_ZERO(&read_fd);
FD_SET(rfd,&read_fd);
FD_SET(fileno(stdin),&read_fd);
net_timer.tv_sec=5;
net_timer.tv_usec=0;
memset(str,0,sizeof(str));
if(i=select(rfd+1,&read_fd,NULL,NULL,&net_timer)==0)
continue;
else if(i<0)
exit(1);
if(FD_ISSET(rfd,&read_fd))
{
if(read(rfd,str,sizeof(str))>0)
printf("A:%s",str);
}
if(FD_ISSET(fileno(stdin),&read_fd))
{
fgets(str,sizeof(str),stdin);
len=write(wfd,str,sizeof(str));
}
}
close(rfd);
close(wfd);
}
- 命名管道(FIFO)聊天程序
- 一个简单的聊天程序--命名管道FIFO
- 命名管道(FIFO)
- 命名管道(FIFO)
- 命名管道(FIFO)
- 命名管道 FIFO(转)
- IPC-命名管道(FIFO)
- FIFO管道/命名管道(半双工)
- 命名管道(FIFO)的实现
- 进程间通信--命名管道(fifo)
- Linux命名管道FIFO
- UNIX命名管道FIFO
- 【Linux】命名管道FIFO
- 命名管道(FIFO)
- Linux 命名管道FIFO
- 命名管道(FIFO) Linux进程进程间的通信之命名管道(FIFO)
- 【进程间通信】普通管道和命名管道(FIFO)
- 深入剖析命名管道FIFO对程序性能的影响
- 让自己的电脑变成虚拟服务器(亲测成功)
- java卡牌插入排序算法
- php中判断是gbk还是utf-8
- 机器学习理论与实战(八)回归
- python的时间处理模块 time
- 命名管道(FIFO)聊天程序
- 透过sax解析浅谈设计模式
- 如何遍历读取文件夹内所有图片并处理
- 堆排序
- POJ 3900 The Robbery (dfs暴搜+剪枝)
- VMware虚拟机安装MAC OS X Mountain Lion详细图文教程
- [c#基础教程]C# 特性(Attribute)详细介绍
- 继承的本质(读《你必须知道的.net》记录笔记)
- hibernate注解的实现原理之1