-
Notifications
You must be signed in to change notification settings - Fork 3
/
client2.c
68 lines (53 loc) · 1.48 KB
/
client2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
** Client 2 process -- reads from a message queue and replies.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
typedef struct msgbuffer {
long mtype;
char mtext[300];
}msgbuf;
int main(void)
{
msgbuf bufrecv;
msgbuf bufsnd;
int msqid;
key_t key;
int count=1;
//get the process id
long processid=getpid();
if ((key = ftok("server.c", 'B')) == -1) { /* same key as server */
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
exit(1);
}
printf("Client 2\n");
printf("Process ID : %ld\n", processid);
printf("Ready to Receive Messages, Message Queue %d.\n", msqid);
while(count>0) {
if (msgrcv(msqid, &bufrecv, sizeof(bufrecv.mtext), processid, 0) == -1) {
perror("msgrcv failed");
exit(1);
}
printf("******************************\n");
printf("Received Message\n");
printf("From: %ld\n", bufrecv.mtype);
printf("Message: \"%s\"\n", bufrecv.mtext);
// scanf("%s", &bufsnd.mtext);
bufsnd.mtype=1;
(void) strcpy(bufsnd.mtext, "George: Thanks Got your Message");
size_t len = strlen(bufsnd.mtext)+1;
if (msgsnd(msqid, &bufsnd, len, 0) == -1) {
perror("msgsnd");
}
}
return 0;
}