Linuxシステムプログラミング:クライアント-サーバがFIFOで通信する

19489 ワード

コードを置いて帰って説明する
ヘッダファイル:
clientinfo.h
1 struct CLIENTINFO{

2     char myfifo[500];

3     int leftarg;

4     int rightarg;

5     char op;

6 }; 

7 typedef struct CLIENTINFO CLIENTINFO, *CLINTINFOPTR;

client.c
 1 #include <unistd.h>

 2 #include <stdio.h>

 3 #include <stdlib.h>

 4 #include <string.h>

 5 #include <sys/types.h>

 6 #include <fcntl.h>

 7 #include <signal.h>

 8 #include "clientinfo.h"

 9 

10 #define FIFO_NAME "/home/levi/chatapplication/data/server_fifo/chat_server_fifo"

11 #define BUFF_SZ 100

12 

13 char mypipename[BUFF_SZ];

14 void handler(int sig){

15     unlink(mypipename);

16     exit(1);

17 }

18 

19 int main(int argc, char const *argv[])

20 {

21     int res;

22     int fifo_fd, my_fifo;

23     int fd;

24     CLIENTINFO info;

25     char buffer[BUFF_SZ];

26 

27     signal(SIGKILL, handler);

28     signal(SIGINT, handler);

29     signal(SIGTERM, handler);

30 

31     if(argc != 4){

32         printf("Usage: %s op1 operation op2
", argv[0]); 33 exit(1); 34 } 35 if(access(FIFO_NAME, F_OK) == -1){ 36 printf("Could not open FIFO %s
", FIFO_NAME); 37 exit(EXIT_FAILURE); 38 } 39 fifo_fd = open(FIFO_NAME, O_WRONLY); 40 if(fifo_fd == -1){ 41 printf("Could not open %s for write access
", FIFO_NAME); 42 exit(EXIT_FAILURE); 43 } 44 sprintf(mypipename, "/home/levi/chat_client_%d_fifo", getpid()); 45 res = mkfifo(mypipename, 0777); 46 if(res != 0){ 47 printf("FIFO %s was not created
", buffer); 48 exit(EXIT_FAILURE); 49 } 50 51 my_fifo = open(mypipename, O_RDONLY | O_NONBLOCK); 52 if(my_fifo == -1){ 53 printf("Could not open %s for read only access
", FIFO_NAME); 54 exit(EXIT_FAILURE); 55 } 56 57 strcpy(info.myfifo, mypipename); 58 info.leftarg = atoi(argv[1]); 59 info.op = argv[2][0]; 60 info.rightarg = atoi(argv[3]); 61 62 write(fifo_fd, &info, sizeof(CLIENTINFO)); 63 close(fifo_fd); 64 65 memset(buffer, '\0', BUFF_SZ); 66 while(1){ 67 res = read(my_fifo, buffer, BUFF_SZ); 68 if(res > 0){ 69 printf("Received from server : %s
", buffer); 70 break; 71 } 72 } 73 printf("Client %d is terminating
", getpid()); 74 close(my_fifo); 75 (void)unlink(mypipename); 76 return 0; 77 }

 
server.c
 1 #include <unistd.h>

 2 #include <stdio.h>

 3 #include <stdlib.h>

 4 #include <string.h>

 5 #include <sys/types.h>

 6 #include <fcntl.h>

 7 #include <signal.h>

 8 #include "clientinfo.h"

 9 

10 #define FIFO_NAME "/home/levi/server_fifo/chat_server_fifo"

11 void handler(int sig){

12     unlink(FIFO_NAME);

13     exit(1);

14 }

15 

16 int calc(CLIENTINFOPTR info){

17     switch(info->op){

18         case '+' : return info->leftarg + info->rightarg;

19         case '-' : return info->leftarg - info->rightarg;

20         case '*' : return info->leftarg * info->rightarg;

21         case '/' : return info->leftarg / info->rightarg;

22     }

23     return 0;

24 }

25 

26 int main(int argc, char const *argv[])

27 {

28     int res, i, fifo_fd, fd1;

29     CLIENTINFO info;

30     char buffer[100];

31     signal(SIGKILL, handler);

32     signal(SIGINT, handler);

33     signal(SIGTERM, handler);

34     if(access(FIFO_NAME, F_OK) == -1){

35         res = mkfifo(FIFO_NAME, 0777);

36         if(res != 0){

37             printf("FIFO %s was not created
", FIFO_NAME); 38 exit(EXIT_FAILURE); 39 } 40 } 41 fifo_fd = open(FIFO_NAME, O_RDONLY); 42 if(fifo_fd == -1){ 43 printf("Could not open %s for read only access
", FIFO_NAME); 44 exit(EXIT_FAILURE); 45 } 46 printf("
Server is rarin to go !
"); 47 while(1){ 48 res = read(fifo_fd, &info, sizeof(CLIENTINFO)); 49 if(res != 0){ 50 printf("Client arrived!!
"); 51 sprintf(buffer, "The result is %d", calc(&info)); 52 fd1 = open(info.myfifo, O_WRONLY | O_NONBLOCK); 53 write (fd1, buffer, strlen(buffer) + 1); 54 close(fd1); 55 } 56 57 } 58 return 0; 59 }

OK!
./server.c &
./client 3 + 5
出力を見て!