andrelievable

serwer

Jan 10th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include <string.h>
  8. #include <signal.h>
  9.  
  10. #define MAX 10
  11. #define SERWER 1
  12.  
  13. int IDqueue;
  14.  
  15. typedef struct message
  16. {
  17.     long msg_one;
  18.     long msg_two;
  19.     char text[MAX];
  20. } Message;
  21.  
  22. int main()
  23. {
  24.     int size;
  25.     int i, j, pid;
  26.     Message msg;
  27.  
  28.     if ((IDqueue = msgget(10, IPC_CREAT | 0600)) == -1)
  29.     {
  30.         printf("Create message queue failed\n");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     printf("ID of queue: %d\n",IDqueue);
  34.  
  35.     while (1)
  36.     {
  37.         printf("SERVER: Waiting on message...\n");
  38.         msg.msg_one = SERWER;
  39.  
  40.         if (msgrcv(IDqueue, &msg, MAX+9, msg.msg_one, MSG_NOERROR) == -1)
  41.         {
  42.             perror("Recieve message failed\n");
  43.             exit(EXIT_FAILURE);
  44.         }
  45.  
  46.         printf("SERVER: Recieve - %s addressed to %ld\n", msg.text, msg.msg_one);
  47.  
  48.         size = strlen(msg.text);
  49.         for ( i = 0; i < size; i++)
  50.         {
  51.             msg.text[i] = toupper(msg.text[i]);
  52.         }
  53.  
  54.         msg.msg_one = msg.msg_two;
  55.         printf("SERVER: Sending - %s ---> %ld\n", msg.text, msg.msg_one);
  56.  
  57.         if ((msgsnd(IDqueue, &msg, strlen(msg.text)+9, 0)) == -1)
  58.         {
  59.             printf("Message added failed\n");
  60.             exit(EXIT_FAILURE);
  61.         }
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment