Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
no
Experiment
date
sign
DISTRIBUTED
SYSTEMS LAB
(IT-407)
Submitted by
Swati Garg
791/IT/13
IT-2
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given key
die("msgget");
//Message Type
sbuf.mtype = 1;
printf("Enter a message to add to message queue : ");
scanf("%[^\n]",sbuf.mtext);
getchar();
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}
else
printf("Message Sent\n");
exit(0);
}
27
perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
/*
*
* Put some things into the memory for the
*
other process to read.
*
*/
s = shm;
/*writing into the memory*/
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
printf("writing in the memory is done\n");
exit(0);
}
//client chat
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#define MAX_BUFFER_SIZE 100
int main()
{
int sockid,newsockid,numbytes;
char sendbuffer[MAX_BUFFER_SIZE+1];
char recvbuffer[MAX_BUFFER_SIZE+1];
int caddrsize;
struct sockaddr_in caddr;
sockid=socket(AF_INET, SOCK_STREAM, 0);
if(sockid==-1)
{
perror("socket");
}
printf("socketid: %d\n",sockid);
memset(&caddr,0,sizeof(caddr));
caddr.sin_family=AF_INET;
caddr.sin_port=htons(8554);
caddr.sin_addr.s_addr=inet_addr("127.0.0.1");
caddrsize=sizeof(caddr);
if(connect(sockid, (struct sockaddr *)&caddr, caddrsize)==-1)
{
perror("connect");
}
/*strcpy(buffer,"connection message");
if(send(sockid,(void *) buffer, strlen(buffer), MSG_DONTWAIT)<0)
{
perror("send");
}*/
while(1)
{
numbytes=recv(sockid, recvbuffer, MAX_BUFFER_SIZE,0);
recvbuffer[numbytes]='\0';
if(numbytes<0)
{
perror("recv");
}
printf("%s\n",recvbuffer);
if(strcmp(recvbuffer,"bye")==0)
{
close(sockid);
break;
}
//printf("send data: ");
gets(sendbuffer);
int bufsize= strlen(sendbuffer);
if(send(sockid,(void *) sendbuffer, bufsize, MSG_DONTWAIT)<0)
{
perror("send");
}
if(strcmp(sendbuffer,"bye")==0)
{
close(sockid);
break;
}
}
close(sockid);
return 0;
}