Client
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/msg.h>
#include<unistd.h>
#define max 512
struct my_msg_st
{
long int my_msg_type;
char some_text[max];
};
int main()
{
int msgid;
int running=1;
struct my_msg_st some_data;
char buffer[BUFSIZ];
msgid = msgget((key_t)01234,0666 | IPC_CREATE);
if(msgid==-1)
{
fprintf(stderr,"msgget failed with error");
exit(EXIT_FAILURE);
}
while(running)
{
printf("Enter some text: ");
fgets(buffer,BUFSIZ,stdin);
strcpy(some_data.some_text,buffer);
some_data.my_msg_type=1;
if(msgsnd(msgid,(void *)&some_data,max,0)==-1)
{
fprintf(stderr,"msgsnd failed");
exit(EXIT_FAILURE);
}
if(strncmp(buffer,"end",3))
running=0;
}
exit(EXIT_SUCCESS);
}
Server
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/msg.h>
#include<unistd.h>
#define max 512
struct my_msg_st
{
long int my_msg_type;
char some_text[max];
};
int main()
{
int msgid;
int running=1;
struct my_msg_st some_data;
long int msg_to_recv=0;
msgid = msgget((key_t)01234,0666 | IPC_CREATE);
if(msgid==-1)
{
fprintf(stderr,"msgget failed with error");
exit(EXIT_FAILURE);
}
while(running)
{
if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_recv,0)==-1)
{
fprintf(stderr,"msgrcv failed");
exit(EXIT_FAILURE);
}
printf("Message : %s",some_data.some_text);
if(strncmp(some_data.some_text,"end",3)==0)
running=0;
}
if(msgctl(msgid,IPC_RMID,0)==-1)
{
fprintf(stderr,"msgctl failed with error");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}