//message queue server
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int msgtype;
char msgfromclient[1024];
int pid;
}msgbuf;
int main()
{
int mqid,n,fd,m1;
mqid=msgget(KEY,0666|IPC_CREAT);
while(1)
{
msgrcv(mqid,&msgbuf,sizeof(msgbuf),1,0);
printf("Filename from client %s\n",msgbuf.msgfromclient);
//sending requested file data to client
fd=open(msgbuf.msgfromclient,O_RDONLY);
n=read(fd,msgbuf.msgfromclient,1024);
msgbuf.msgtype=msgbuf.pid;
msgbuf.pid=getpid();
msgsnd(mqid,&msgbuf,sizeof(msgbuf),0);
}
return 0;
}
//client
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 500
struct msg
{
long int type;
char a[1024];
int pid;
}p,p1;
int main()
{
int m;
m=msgget(KEY,0);
//fill the structure to send data
p.type=1;
printf("\nEnter the msg");
scanf("%s",&p.a);
pid_t pid;
p.pid=getpid();
msgsnd(m,&p,sizeof(p),0);
msgrcv(m,&p1,sizeof(p),p.pid,0);
printf("%s",p1.a);
return 0;
}
//output:
ServerTerminal$ ./server
ClientTerminal$ ./client
Enter the msg hello.txt
ServerTerminal$
Filename from client hello
ClientTerminal$ ./client
contents of hello.txt
No comments:
Post a Comment