-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-7.c
63 lines (55 loc) · 973 Bytes
/
10-7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#define FIFO_READ "readfifo"
#define FIFO_WRITE "writefifo"
#define BUF_SIZE 1024
int main()
{
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;
}