Advertisement
Guest User

Client

a guest
Oct 16th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <time.h>
  8. #include <pthread.h>
  9.  
  10. #define FIFO_SERVER "FIFO_SERVER"
  11. #define FIFO_CLIENT "MYFIFO"
  12. static bool flag = true;
  13. void* threadFunc(void* thread_data){
  14.     sleep(30);
  15.     flag = false;
  16. }
  17. int main()
  18. {
  19.     unlink(FIFO_CLIENT);
  20.     FILE* fp;
  21.     char readbuf[80];
  22.  
  23.     if((fp = fopen(FIFO_SERVER, "w")) == NULL)
  24.     {
  25.         perror("open error");
  26.         exit(1);
  27.     }
  28.     umask(0); // set file mask for creating files
  29.     mknod(FIFO_CLIENT, S_IFIFO | 0777, 0); // create FIFO file
  30.    
  31.     // send data to server
  32.     fputs(FIFO_CLIENT, fp);
  33.     fclose(fp);
  34.    
  35.  
  36.     // read echo replay from server
  37.     int count = 0;
  38.     if((fp = fopen(FIFO_CLIENT, "r")) == NULL)
  39.     {
  40.         perror("open error");
  41.         exit(1);
  42.     }
  43.     pthread_t t1;
  44.     pthread_create(&t1, NULL, threadFunc, NULL);
  45.     while(flag){
  46.         fgets(readbuf,1,fp);
  47.         count++;
  48.     }
  49.     printf("Count symbols: %d\n", count);
  50.     fclose(fp);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement