Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/msg.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <signal.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <ctype.h>
  11. #include <unistd.h>
  12.  
  13. #define MAX 50
  14.  
  15. typedef struct Message {
  16. long type;
  17. long pidClient;
  18. char text[MAX];
  19. } Message;
  20.  
  21.  
  22. key_t key;
  23. int idQueue;
  24. Message msgR;
  25. Message msgS;
  26. char messageText[MAX];
  27.  
  28. void *receiving() {
  29. while(1) {
  30. msgR.type = getpid();
  31. //memset(msgR.text, NULL, MAX);
  32. int i;
  33. for(i = 0; i < MAX; i++) {
  34. msgR.text[i] = '\0';
  35. }
  36. if((msgrcv(idQueue, (Message *)&msgR, MAX+sizeof(long), msgR.type, 0)) == -1) {
  37. if (errno == 43) {
  38. printf("[K: %d] Serwer zakonczyl prace! Kolejka zostala usunieta\n", getpid());
  39. exit(2);
  40. } else if (errno == 22) {
  41. printf("[K: %d] Blad przy pobieraniu komunikatu od serwera! Serwer zakonczyl prace.\n", getpid());
  42. exit(2);
  43. } else {
  44. printf("[K: %d] Blad przy pobieraniu komunikatu od serwera! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  45. exit(2);
  46. }
  47. } else
  48. printf("[K: %d] Klient odebral wiadomosc. Tresc: %s\n", getpid(), msgR.text);
  49. }
  50. pthread_exit((void *) 0);
  51. }
  52.  
  53. void *sending() {
  54. while(1) {
  55. msgS.type = 1;
  56. msgS.pidClient = getpid();
  57. // sleep(1);
  58. printf("[K %d] Wysyłanie wiadomosci! Podaj tekst do wyslania: \n", getpid());
  59. int i = 0;
  60. while(i < MAX) {
  61. messageText[i] = getchar();
  62. if(messageText[i] == '\n') {
  63. messageText[i] = '\0';
  64. break;
  65. }
  66. i++;
  67. }
  68. strcpy(msgS.text, messageText);
  69. //printf("Dlugosc tekstu: strlen = %d, i = %d", strlen(msgS.text), i);
  70. if((msgsnd(idQueue, (Message *)&msgS, strlen(msgS.text)+sizeof(long), 0)) == -1) {
  71. printf("[K %d] Blad przy wysylaniu wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  72. exit(2);
  73. } else
  74. printf("\n[K %d] Klient wyslal wiadomosc do serwera. Tresc wiadomosci: %s\n", getpid(), msgS.text);
  75. }
  76. pthread_exit((void *) 0);
  77. }
  78.  
  79. int main() {
  80. pthread_t TID1, TID2;
  81. if((key = ftok(".", 111213)) == -1) {
  82. printf("[K %d] Blad tworzenia klucza!\n", getpid());
  83. exit(-1);
  84. } else
  85. printf("[K %d] Klucz zostal utworzony\n", getpid());
  86.  
  87. // key = 111213;
  88. if((idQueue = msgget(808182, 0666 | IPC_CREAT)) == -1 ) {
  89. printf("[K %d] Blad przy tworzeniu kolejki komunikatow! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  90. exit(-2);
  91. }
  92. printf("[K %d] Utworzono kolejke komunikatow\n", getpid());
  93.  
  94.  
  95. if(pthread_create(&TID1, NULL, receiving, NULL) == -1) {
  96. printf("[K %d] Blad tworzenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  97. exit(-1);
  98. }
  99.  
  100. if(pthread_create(&TID2, NULL, sending, NULL) == -1) {
  101. printf("[K %d] Blad tworzenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  102. exit(-1);
  103. }
  104. if(pthread_join(TID1, NULL) == -1) {
  105. printf("[K %d] Blad przylaczenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  106. exit(-1);
  107. }
  108.  
  109. if(pthread_join(TID2, NULL) == -1) {
  110. printf("[K %d] Blad przylaczenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
  111. exit(-1);
  112. }
  113. printf("[K %d] Klient skonfigurowany. Prosze zaczac podawanie wiadomosci!\n", getpid());
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement