Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/shm.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10. char buff[100];
  11. char dim[2];
  12. unsigned char size;
  13.  
  14. int main()
  15. {
  16. mkfifo("RESP_PIPE_50157",0600);
  17. int ok=1;
  18.  
  19. int fd=open("REQ_PIPE_50157",O_RDONLY);
  20.  
  21. if(fd<0)
  22. {
  23. perror("Error opening pipe");
  24. ok=0;
  25. }
  26.  
  27. int myfd=open("RESP_PIPE_50157",O_RDWR);
  28. if(myfd<0)
  29. {
  30. perror("Error opening pipe2");
  31. ok=0;
  32. }
  33. //write(myfd,"07434f4e4e454354",16);
  34. if(ok==1)
  35. {
  36. printf("SUCCESS");
  37. size=strlen("CONNECT");
  38. write(myfd,&size,sizeof(unsigned char));
  39. write(myfd,"CONNECT",7);
  40. }
  41.  
  42. while(strcmp(buff,"EXIT")!=0)
  43. {
  44. read(fd,&dim,sizeof(unsigned char));
  45. read(fd,buff,*dim);
  46. if(strcmp(buff,"PING")==0)
  47. {
  48. size=strlen("PING");
  49. write(myfd,&size,sizeof(unsigned char));
  50. write(myfd,"PING",4);
  51. size=strlen("PONG");
  52. write(myfd,&size,sizeof(unsigned char));
  53. write(myfd,"PONG",4);
  54. int nr=50157;
  55. write(myfd,&nr,sizeof(nr));
  56. }
  57.  
  58. if(strcmp(buff,"CREATE_SHM")==0)
  59. {
  60. unsigned int memsize=0;
  61. int ok1=1;
  62.  
  63. size=strlen("CREATE_SHM");
  64. write(myfd,&size,sizeof(unsigned char));
  65. write(myfd,"CREATE_SHM",4);
  66.  
  67. read(myfd,&memsize,sizeof(unsigned int));
  68. int shmid=shmget(15603,memsize, IPC_CREAT|664);
  69.  
  70. if(shmid<0)
  71. {
  72. size=strlen("ERROR");
  73. write(myfd,&size,sizeof(unsigned char));
  74. write(myfd,"ERROR",5);
  75. ok1=0;
  76. }
  77. int* sharedMem=(int*)shmat(shmid,NULL,0);
  78.  
  79. if(sharedMem<0)
  80. {
  81. size=strlen("ERROR");
  82. write(myfd,&size,sizeof(unsigned char));
  83. write(myfd,"ERROR",5);
  84. ok1=0;
  85. }
  86.  
  87. if(ok1==1)
  88. {
  89. size=strlen("SUCCESS");
  90. write(myfd,&size,sizeof(unsigned char));
  91. write(myfd,"SUCCESS",7);
  92. }
  93.  
  94.  
  95.  
  96. }
  97.  
  98.  
  99. if(strcmp(buff,"EXIT")==0)
  100. {
  101. close(fd);
  102. // unlink("RESP_PIPE_50157");
  103. close(myfd);
  104.  
  105. }
  106.  
  107.  
  108. }
  109. // close(fd);
  110. //close(myfd);
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement