Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
1.1 kB
11
Indexable
Never
//client
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 100

struct msg{
	long int type;
	char a[1024];
	int pid;
}p1,p2;
int main(){
	int m;
	m=msgget(KEY,0);
	p1.type=1;
	while(1){
		printf("\nEnter msg: ");
		scanf("%s",&p1.a);
		pid_t pid;
		p1.pid=getpid();
		msgsnd(m,&p1,sizeof(p1),0);
		msgrcv(m,&p2,sizeof(p1),p1.pid,0);
		printf("\nMessage from server: %s\n",p2.a);
	}
	return 0;
}




//server
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#define KEY 100

struct msg{
	long int type;
	char a[1024];
	int pid;
}p;
int main(){
	int m,n,fd,m1;
	m=msgget(KEY,0666|IPC_CREAT);
	while(1){
		msgrcv(m,&p,sizeof(p),1,0);
		printf("\nMessage from client: %s\n",p.a);
		fd=open(p.a,O_RDONLY);
		n=read(fd,p.a,1024);
		p.type=p.pid;
		p.pid=getpid();
		printf("\nEnter msg: ");
		scanf("%s",&p.a);
		msgsnd(m,&p,sizeof(p),0);
	}
	return 0;
}
Leave a Comment