A能随时发信息给B,B能随时接收A发送的数据,反之亦然。
A端
#include<stdio.h>
#include<sys/types.h>
#include<errno.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<sys/wait.h>
void* bad1(void * arg)
{
char arr[128]="";
ssize_t res=0;
umask(0);
if(mkfifo("./FIFO1",0777));
{
if(errno!=17)
{
perror("mkfifo");
return NULL;
}
}
printf("mkfifo success\n");
int fd=open("./FIFO1",O_WRONLY);
while(1)
{
if(fd<0)
{
perror("open");
return NULL;
}
printf("A >>>");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]=0;
res=write(fd,arr,sizeof(arr));
if(res==0||strcasecmp(arr,"quit")==0)
{
printf("对端退出\n");
close(fd);
break;
}
if(res<0)
{
perror("write");
return NULL;
}
}
close(fd);
return NULL;
}
void* bad2(void * arg)
{
char arr[128]="";
ssize_t res=0;
umask(0);
if(mkfifo("./FIFO2",0777));
{
if(errno!=17)
{
perror("mkfifo");
return NULL;
}
}
printf("mkfifo success\n");
int fd=open("./FIFO2",O_RDONLY);
while(1)
{
if(fd<0)
{
perror("open");
return NULL;
}
bzero(arr,sizeof(arr));
res=read(fd,arr,sizeof(arr));
if(res<0)
{
perror("read");
return NULL;
}
if(res==0||strcasecmp(arr,"quit")==0)
{
printf("对端退出\n");
close(fd);
break;
}
printf("\n");
printf("B: %s\n",arr);
printf("A >>>");
fflush(stdout);
}
close(fd);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1;
pthread_t tid2;
int ttid1=pthread_create(&tid1,NULL,bad1,NULL);
int ttid2=pthread_create(&tid2,NULL,bad2,NULL);
if(ttid1)
{
fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
return -1;
}
pthread_detach(tid1);
if(ttid2)
{
fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
return -1;
}
pthread_join(tid2,NULL);
wait(NULL);
return 0;
}
B端
#include<stdio.h>
#include<sys/types.h>
#include<errno.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<sys/wait.h>
void* bad1(void * arg)
{
char arr[128]="";
ssize_t res=0;
umask(0);
if(mkfifo("./FIFO1",0777));
{
if(errno!=17)
{
perror("mkfifo");
return NULL;
}
}
printf("mkfifo success\n");
int fd=open("./FIFO1",O_RDONLY);
while(1)
{
if(fd<0)
{
perror("open");
return NULL;
}
bzero(arr,sizeof(arr));
res=read(fd,arr,sizeof(arr));
if(res<0)
{
perror("read");
return NULL;
}
if(res==0||strcasecmp(arr,"quit")==0)
{
printf("对端退出\n");
close(fd);
break;
}
printf("\n");
printf("A: %s\n",arr);
printf("B >>>");
fflush(stdout);
}
close(fd);
return NULL;
}
void* bad2(void * arg)
{
char arr[128]="";
ssize_t res=0;
umask(0);
if(mkfifo("./FIFO2",0777));
{
if(errno!=17)
{
perror("mkfifo");
return NULL;
}
}
printf("mkfifo success\n");
int fd=open("./FIFO2",O_WRONLY);
while(1)
{
if(fd<0)
{
perror("open");
return NULL;
}
printf("B >>>");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]=0;
res=write(fd,arr,sizeof(arr));
if(res==0||strcasecmp(arr,"quit")==0)
{
printf("对端退出\n");
close(fd);
break;
}
if(res<0)
{
perror("write");
return NULL;
}
}
close(fd);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1;
pthread_t tid2;
int ttid1=pthread_create(&tid1,NULL,bad1,NULL);
int ttid2=pthread_create(&tid2,NULL,bad2,NULL);
if(ttid1)
{
fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
return -1;
}
pthread_detach(tid1);
if(ttid2)
{
fprintf(stderr,"pthread_creat is flase.%d\n",__LINE__);
return -1;
}
pthread_join(tid2,NULL);
wait(NULL);
return 0;
}