Thursday, July 14, 2011

Server program to recieve File using msg Q's IPC

/* msgrecvQ.c */

# include <stdio.h>
# include <string.h>
# include <sys/stat.h>
# include <sys/msg.h>
# include <sys/ipc.h>
# include <sys/types.h>

//message structure

struct message
{
long msg_type;
char msg_data[1024];
};
int main(int argc,char*argv[])
{
FILE*f;
int x,y,i=0;
char ch;
struct message msg_obj2={100,"M"};
int msg_id;

//creating msg q id
if((msg_id=msgget(1234,0644))==-1)
perror("\nUnable to get message id...");
else
printf("\nmsgid=%d",msg_id);

/* rec message from q*/
if((y=msgrcv(msg_id,&msg_obj2,1024,100,MSG_NOERROR))==-1)
perror(":- msgrcv error...");
else
printf("\nRec Bytes : %d",y);

if((f=fopen(argv[1],"w"))==NULL)
perror("\nUnable to open file for writing...");
else
{
for(i=0;msg_obj2.msg_data[i]!=0;i++)
{
putc(msg_obj2.msg_data[i],f);
}
fclose(f); //closing file
}
return 0;
}

No comments:

Post a Comment