Thursday, July 14, 2011

Client file for file sending using IPC Q's

/* msgsendQ.c */

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

struct message
{
long msg_type;
char msg_data[1024];
};

int main(int arc,char*argv[])
{
//message structure
FILE*f;
int x,y,i=0;
char ch;
int key=1234;
int msg_id;
struct message msg_obj1={100,"I"};

//function to put file to message Q
if((f=fopen(argv[1],"r"))==NULL)
perror("\nUnable to open file for reading...");
else
{
while((ch=getc(f))!=EOF)
{
msg_obj1.msg_data[i++]=ch; //writing to msg_data
}
fclose(f); //closing file
}

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

/*writing message to the q */
if((x=msgsnd(msg_id,&msg_obj1,strlen(msg_obj1.msg_data),IPC_NOWAIT))==-1)
perror("\nUnable to send message...");
else
printf("\nSend Msg Success : return %d",x);

return 0;
}

No comments:

Post a Comment