Advertisement
d1cor

mq1_a.c

Sep 27th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. /*******************************************
  2. * Author     : Diego Cordoba / @d1cor      *
  3. * Contact    : juncotic.com / um.edu.ar    *
  4. *******************************************/
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<sys/types.h>
  9. #include<sys/stat.h>
  10. #include<unistd.h>
  11. #include<string.h>
  12. #include<fcntl.h>
  13. #include<errno.h>
  14. #include<sys/ipc.h>
  15. #include<sys/msg.h>
  16. #include<mqueue.h>
  17. #define MSG_SIZE 128
  18.  
  19. #define SERVER_QUEUE_NAME   "/test"
  20. #define QUEUE_PERMISSIONS 0660
  21. #define MAX_MESSAGES 10
  22. #define MAX_MSG_SIZE 256
  23. #define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10
  24.  
  25.  
  26.  
  27. int main(int argc, char** argv) {
  28.  
  29.     char *error;
  30.  
  31.     mqd_t msg_id;
  32.  
  33.     struct mq_attr attr;
  34.     mq_unlink(SERVER_QUEUE_NAME);
  35.  
  36.     attr.mq_flags = 0;
  37.     attr.mq_maxmsg = MAX_MESSAGES;
  38.     attr.mq_msgsize = MAX_MSG_SIZE;
  39.     attr.mq_curmsgs = 0;
  40.  
  41.     if ((msg_id = mq_open (SERVER_QUEUE_NAME, O_RDONLY | O_CREAT, QUEUE_PERMISSIONS, &attr)) == -1) {
  42.         perror ("Server: mq_open (server)");
  43.         exit (1);
  44.     }
  45.  
  46.     char buffer [MSG_BUFFER_SIZE];
  47.  
  48.     while (mq_receive (msg_id, buffer, MSG_BUFFER_SIZE, NULL)) {
  49.         printf("Server message received: %s\n", buffer);
  50.         memset(buffer, 0, MSG_BUFFER_SIZE);
  51.     }
  52.  
  53.  
  54.     mq_unlink(SERVER_QUEUE_NAME);
  55.  
  56.  
  57.     return 0;
  58.  
  59. err:
  60.     fprintf(stderr,"ERROR %s (%d) - %s\n",error,errno,strerror(errno));
  61.     exit(errno);
  62. }
  63.  
  64. //  https://tmate.io/t/ro-pBYOPTf8HfhYVxmXEFGuTR8B1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement