-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
79 lines (63 loc) · 1.59 KB
/
client.cpp
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
69
70
71
72
73
74
75
76
77
78
79
/** This program illustrates the client end of the message queue **/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <vector>
#include <list>
#include <string>
/* Defines message queue related functions */
#include "msg.h"
using namespace std;
int main()
{
/* Use a random file and a random character to generate
* a unique key. Same parameters to this function will
* always generate the same value. This is how multiple
* processes can connect to the same queue.
*/
key_t key = ftok("/bin/ls", 'O');
/* Was the key allocation successful ? */
if(key < 0)
{
perror("ftok");
exit(-1);
}
/* Connect to the message queue; fail if the
* there is no message queue associated with
* this key. This function returns the id of
* the queue.
*/
int msqid = connectToMessageQueue(key);
/* The message */
message msg;
while(true)
{
/* Generate 50 requests */
for(int reqNum = 0; reqNum < 50; ++reqNum)
{
/* Generate 100 requests and send them */
msg.id = rand() % 1000;
/* Send a message to the server */
msg.messageType = CLIENT_TO_SERVER_MSG;
/* Send the message */
sendMessage(msqid, msg);
}
/* Receive the responses for the 50 requests */
for(int reqNum = 0; reqNum < 50; ++reqNum)
{
/* Receive the message */
recvMessage(msqid, msg, SERVER_TO_CLIENT_MSG);
/* Print only existing records */
if(msg.id != -1)
/* Print the message */
msg.print(stderr);
}
}
return 0;
}