#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>


#define MAX_TEXT 512

struct msg_st
{
    int msg_type;
    char msg_text[MAX_TEXT];
};

int main()
{
    struct msg_st recdata;
    int key, msgid;
    if((key=ftok(".",512))==-1)
    {
	perror("ftok");
	exit(1);
    }
    if((msgid=msgget(key,0666))==-1)
    {
	perror("msgget");
	exit(1);
    }
    while(1)
    {
	if(msgrcv(msgid,(void *)&recdata,512,0,0)==-1)
	{
	    perror("msgrcv");
	    exit(1);
	}
	printf("message received is :%s\n",recdata.msg_text);
	
	if(strcmp(recdata.msg_text,"bye")==0)   
	    break;
    }
    if(msgctl(msgid,IPC_RMID,0)==-1)
    {

	perror("msgctl");
	exit(0);
    }
    return 0;
}
