Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <string.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "header.h"
  7. #include <signal.h>
  8. #include <sys/msg.h>
  9. #include <sys/ipc.h>
  10. #include <errno.h>
  11.  
  12.  
  13. int client_queue = -1;
  14. int server_queue = -1;
  15.  
  16. int send_message(Message m) {
  17.     int status = msgsnd(server_queue, &m, SIZE_OF_MESSAGE_STRUCTURE, 0);
  18.     if (status == -1)
  19.         printf("Do server_queue: %d nie udalo wyslac sie wiadomosci. %s\n", server_queue, strerror(errno));
  20.     else
  21.         printf("Wyslano wiadomosc do server_queue: %d\n", server_queue);
  22.     return status;
  23. }
  24.  
  25. int receiveReply() {
  26.     Message m;
  27.     if (msgrcv(client_queue, &m, SIZE_OF_MESSAGE_STRUCTURE, 0, 0) == -1) {
  28.         printf("Some problem with receiving reply!\n");
  29.         exit(1);
  30.     }
  31.     printf("Server reply positive with message: %s\n", m.mes);
  32. }
  33.  
  34. void sigint_handler(int sig_num){
  35.     printf("Kleint %d konczy dzialanie.\n", getpid());
  36.     exit(0);
  37. }
  38.  
  39. void client_closing(){
  40.     if(client_queue != -1)
  41.         if( msgctl(client_queue, IPC_RMID, NULL) == -1)
  42.             printf("Wystapil problem z zamykaniem kolejki.\n");
  43.         else
  44.             printf("Kolejka zamknieta poprawnie. Klient %d zakonczyl prace.\n", getpid());
  45.     else
  46.         printf("Klient %d zakonczyl prace. Kolejka nie istanila.\n", getpid());
  47. }
  48.  
  49. int main(int argc, char** argv) {
  50.     atexit(client_closing);
  51.     signal(SIGINT, sigint_handler);
  52.  
  53.     if ((server_queue = msgget(ftok(getenv("HOME"), 's'), 0)) == -1) {
  54.         printf("Nie udalo otrzymac sie kolejki serwera.\n");
  55.         exit(2);
  56.     }
  57.  
  58.     key_t client_key = ftok(getenv("HOME"),'s');
  59.     if ((client_queue = msgget(client_key, IPC_CREAT | 0600)) == -1) {
  60.         printf("Nie udalo storzyc sie kolejki. Moje pid: \n", getpid());
  61.         exit(2);
  62.     } else
  63.         printf("Udalo sie polaczyc, czekam na potwierdzenie\n");
  64.  
  65.     Message m;
  66.     m.pid = getpid();
  67.     m.mtype = INIT_CLIENT;
  68.     sprintf(m.mes, "%d", client_key);
  69.  
  70.     if(send_message(m) == -1);
  71.         exit(2);
  72.  
  73.     printf("Udalo polaczyc sie do serwera.\n");
  74.  
  75.     while (1) {
  76.         printf("Rozpocznij wiadomosc od cyfry definujacej usluge i ' ':\n");
  77.         printf("ECHO - 1\n");
  78.         printf("UPPERCASE - 2\n");
  79.         printf("TIME - 3\n");
  80.         printf("END - 4\n");
  81.         char cbuf[MESSAGE_SIZE];
  82.         fgets(cbuf, MESSAGE_SIZE, stdin);
  83.         int mtype = atoi(strtok(cbuf, " "));
  84.         char *msg = " ";
  85.         if(mtype == 1 || mtype == 2)
  86.             msg = strtok(NULL, " ");
  87.  
  88.         if(strlen(msg) > MESSAGE_SIZE){
  89.             printf("You mtype too long message. Try again...\n");
  90.             continue;
  91.         }
  92.  
  93.         m.mtype = mtype;
  94.         sprintf(m.mes, "%s", msg);
  95.  
  96.         send_message(m);
  97.  
  98.         if(mtype == 4)
  99.             break;
  100.  
  101.         receiveReply();
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement