Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <string.h>
- #define SERVER 1L
- typedef struct {
- long msg_to;
- long msg_fm;
- char buffer[BUFSIZ];
- } MESSAGE;
- int mid,n;
- key_t key;
- char buff[50];
- struct msqid_ds buf;
- MESSAGE msg;
- int main(int argc, char** argv) {
- key = ftok(".", 'z');
- mid = msgget(key, 0);
- //Aquiring Message Queue ID
- printf("Message Queue ID: %d\n", mid);
- printf("user1 ID: %ld\n", (long)getpid());
- printf("enter the msg u want to send\n");
- scanf("%s",buff);
- n=strlen(buff);
- int i = 0;
- for(i=0;i<=n;i++)
- {
- msg.buffer[i] = buff[i];
- }
- //Getting user1 PID and preparing message to message queue
- long iD = (long)getpid();
- msg.msg_to = SERVER;
- msg.msg_fm = (long)getpid();
- //Send message to Message Queue for user2, throws and error for invalid input
- if(msgsnd(mid, &msg, sizeof(msg.buffer), 0)==-1){
- perror("msgsnd");
- exit(-1);
- }
- //Client waits for response from Server, throws an error if invalid input
- if(msgrcv(mid, &msg, sizeof(msg), iD, 0)<0){
- perror("msgrcv");
- exit(-1); }
- //Displaying the message received .
- printf("Message recieved\n");
- printf("%s\n", msg.buffer);
- //Removing message queue
- msgctl(mid, IPC_RMID, (struct msqid_ds *) 0);
- //user1 exits
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment