Linux FIFO

head.h:

 1 /* head */
 2 #include <unistd.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 #include <sys/types.h>
 7 #include <fcntl.h>
 8 #include <signal.h>
 9 #include <sys/stat.h>
10 
11 #define SERVER_FIFO "/tmp/server_fifo"
12 #define LOGIN    0
13 #define SEND    1
14 
15 typedef struct 
16 {
17     int service_type;
18     char myfifo[50];
19 
20     union {
21         struct {
22             char username[50];
23         }user ;
24         struct {
25             char name_src[50];
26             char name_dst[50];
27             char msg[100];
28         }msg;
29     } choose;
30 }Message;
31 
32 typedef struct {
33     char name[50];
34     char fifo[50];
35 }User;

server.c

 1 #include "head.h"
 2 #define BUF_SIZE 100
 3 
 4 int ser_fifo;
 5 int i;
 6 int fd;
 7 int user_num ;
 8 char buffer[BUF_SIZE];
 9 Message message;
10 User users[10];
11 
12 void init(){
13     if(access(SERVER_FIFO, F_OK) == -1){
14         if(mkfifo(SERVER_FIFO, 0777) == -1){
15             perror("init-access-mkfifo");
16             exit(1);
17         }
18     }
19 
20     if((ser_fifo = open(SERVER_FIFO, O_RDONLY | O_NONBLOCK)) == -1){
21         perror("init-open");
22         exit(1);
23     }
24 }
25 
26 
27 void sendBuffer(){
28 
29     if((fd = open(message.myfifo, O_WRONLY | O_NONBLOCK)) == -1){
30         perror("sendBuffer-open");
31         exit(1);
32     }
33     if(write(fd, buffer, strlen(buffer)+1) == -1){
34         perror("serndBuffer-write");
35     }
36     close(fd);
37 }
38 
39 void login(){
40     for (i = 0; i < user_num; ++i)
41     {
42         if(strcmp(message.choose.user.username, users[i].name) == 0){
43             strcpy(buffer,"1,the user has existed");
44             break;
45         }
46     }
47 
48     if(i == user_num)
49         strcpy(buffer ,"0,login success");
50     strcpy(users[user_num].name, message.choose.user.username);
51     strcpy(users[user_num++].fifo, message.myfifo);
52 
53     sendBuffer();
54 }
55 
56 void send(){
57     for (i = 0; i < user_num; ++i)
58     {
59         if(strcmp(message.choose.msg.name_dst, users[i].name) == 0)
60             break;
61     }
62     if(i == user_num)
63         strcpy(buffer, "the user is not exist! i=user_num");
64     else{
65         if((fd = open(users[i].fifo, O_WRONLY | O_NONBLOCK)) == -1){
66             perror("send-open");
67             exit(1);
68         }
69         sprintf(buffer, "from %s: %s\n", message.choose.msg.name_src,message.choose.msg.msg);
70         if(write(fd, buffer, BUF_SIZE) == -1){
71             perror("send-write");
72             exit(1);
73         }
74         close(fd);
75         strcpy(buffer, "send success !!");
76     }
77     sendBuffer();
78 }
79 
80 void operation(int type){
81     if(type == LOGIN) login();
82     else if(type == SEND) send();
83 }
84 
85 int main(int argc, char const *argv[])
86 {
87     init();
88     while(1){
89         if((read(ser_fifo, &message, sizeof(Message) + 50))>0){
90             operation(message.service_type);
91         }
92     }
93     return 0;
94 }

client.c:

  1 #include "head.h"
  2 #define BUF_SIZE 100
  3 char mypipename[100];
  4 char myname[50];
  5 char buffer[BUF_SIZE];
  6 int ser_fifo;
  7 int my_fifo;
  8 Message message;
  9 
 10 
 11 void init(){
 12     if(access(SERVER_FIFO, F_OK) == -1){
 13         perror("init-access");
 14         exit(1);
 15     }
 16 
 17     ser_fifo = open(SERVER_FIFO, O_WRONLY);
 18     if(ser_fifo == -1){
 19         perror("init-open");
 20         exit(1);
 21     }
 22 
 23     sprintf(mypipename, "/tmp/client%d_fifo", getpid());
 24 //    printf("%s",mypipename);
 25     if(mkfifo(mypipename, 0777) == -1){
 26         perror("init-mkfifo");
 27         exit(1);
 28     }
 29 
 30     my_fifo = open(mypipename, O_RDONLY | O_NONBLOCK);
 31     if(my_fifo == -1){
 32         perror("open my fifo");
 33         exit(1);
 34     }
 35 
 36     strcpy(message.myfifo, mypipename);
 37 }
 38 
 39 void sendMessage(){
 40     if(write(ser_fifo, &message, sizeof(Message)) == -1){
 41         perror("sendMessage-write");
 42         exit(1);
 43     }
 44 }
 45 
 46 recMessage(){
 47     int res;
 48     if((res = read(my_fifo, buffer, BUF_SIZE)) == -1){
 49         perror("recMessage-read");
 50         exit(1);
 51     }
 52     return res;
 53 }
 54 
 55 int login(){
 56     message.service_type = LOGIN;
 57     printf("username: ");
 58     scanf("%s", message.choose.user.username);
 59     sendMessage();
 60     while(recMessage() == 0) ;
 61     printf("%s\n", buffer);
 62     if(buffer[0] == '0'){
 63         strcpy(myname, message.choose.user.username);
 64         return 0;
 65     }
 66     return -1;
 67 }
 68 
 69 void send(){
 70     message.service_type = SEND;
 71     printf("message to : ");
 72     scanf("%s", message.choose.msg.name_dst);
 73     printf("The message is :\n");
 74     getchar();
 75     fgets(message.choose.msg.msg, 100, stdin);
 76     strcpy(message.choose.msg.name_src, myname);
 77     sendMessage();
 78 }
 79 
 80 int main(int argc, char const *argv[])
 81 {
 82     init();
 83     while(login()!= 0);
 84     int pid;
 85     if((pid = fork()) == -1){
 86         perror("main-fork");
 87         exit(1);
 88     }
 89 
 90     if(pid == 0){
 91         while(1){
 92             if(recMessage() > 0)
 93                 printf("%s\n", buffer);
 94         }
 95     }
 96     else{
 97         while(1){
 98             sleep(1);
 99             send();
100         }
101     }
102     return 0;
103 }
原文地址:https://www.cnblogs.com/firstrate/p/3120233.html