Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/msg.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <signal.h>
- #include <errno.h>
- #include <string.h>
- #include <pthread.h>
- #include <ctype.h>
- #include <unistd.h>
- #define MAX 50
- typedef struct Message {
- long type;
- long pidClient;
- char text[MAX];
- } Message;
- key_t key;
- int idQueue;
- Message msgR;
- Message msgS;
- char messageText[MAX];
- void *receiving() {
- while(1) {
- msgR.type = getpid();
- //memset(msgR.text, NULL, MAX);
- int i;
- for(i = 0; i < MAX; i++) {
- msgR.text[i] = '\0';
- }
- if((msgrcv(idQueue, (Message *)&msgR, MAX+sizeof(long), msgR.type, 0)) == -1) {
- if (errno == 43) {
- printf("[K: %d] Serwer zakonczyl prace! Kolejka zostala usunieta\n", getpid());
- exit(2);
- } else if (errno == 22) {
- printf("[K: %d] Blad przy pobieraniu komunikatu od serwera! Serwer zakonczyl prace.\n", getpid());
- exit(2);
- } else {
- printf("[K: %d] Blad przy pobieraniu komunikatu od serwera! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(2);
- }
- } else
- printf("[K: %d] Klient odebral wiadomosc. Tresc: %s\n", getpid(), msgR.text);
- }
- pthread_exit((void *) 0);
- }
- void *sending() {
- while(1) {
- msgS.type = 1;
- msgS.pidClient = getpid();
- // sleep(1);
- printf("[K %d] Wysyłanie wiadomosci! Podaj tekst do wyslania: \n", getpid());
- int i = 0;
- while(i < MAX) {
- messageText[i] = getchar();
- if(messageText[i] == '\n') {
- messageText[i] = '\0';
- break;
- }
- i++;
- }
- strcpy(msgS.text, messageText);
- //printf("Dlugosc tekstu: strlen = %d, i = %d", strlen(msgS.text), i);
- if((msgsnd(idQueue, (Message *)&msgS, strlen(msgS.text)+sizeof(long), 0)) == -1) {
- printf("[K %d] Blad przy wysylaniu wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(2);
- } else
- printf("\n[K %d] Klient wyslal wiadomosc do serwera. Tresc wiadomosci: %s\n", getpid(), msgS.text);
- }
- pthread_exit((void *) 0);
- }
- int main() {
- pthread_t TID1, TID2;
- if((key = ftok(".", 111213)) == -1) {
- printf("[K %d] Blad tworzenia klucza!\n", getpid());
- exit(-1);
- } else
- printf("[K %d] Klucz zostal utworzony\n", getpid());
- // key = 111213;
- if((idQueue = msgget(808182, 0666 | IPC_CREAT)) == -1 ) {
- printf("[K %d] Blad przy tworzeniu kolejki komunikatow! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-2);
- }
- printf("[K %d] Utworzono kolejke komunikatow\n", getpid());
- if(pthread_create(&TID1, NULL, receiving, NULL) == -1) {
- printf("[K %d] Blad tworzenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_create(&TID2, NULL, sending, NULL) == -1) {
- printf("[K %d] Blad tworzenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_join(TID1, NULL) == -1) {
- printf("[K %d] Blad przylaczenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_join(TID2, NULL) == -1) {
- printf("[K %d] Blad przylaczenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- printf("[K %d] Klient skonfigurowany. Prosze zaczac podawanie wiadomosci!\n", getpid());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement