-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
62 lines (50 loc) · 1.44 KB
/
client.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
#include "common.h"
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
/* Bradley Schwarz
* Bjschwa2
*
* This program is the client that
* communicates using POSIX message queues
*/
// Print out an error message and exit.
static void fail( char const *message ) {
fprintf( stderr, "%s\n", message );
exit( 1 );
}
int main(int argc, char *argv[]) {
if ( argc != 3 )
fail( "usage: client <add/remove/query> <word>" );
// make arguments into char
char *word = argv[2];
char *para = argv[1];
// Open the message queue for talking to the server.
mqd_t serverQueue = mq_open( SERVER_QUEUE, O_WRONLY );
mqd_t clientQueue = mq_open( CLIENT_QUEUE, O_RDONLY );
if ( serverQueue == -1 || clientQueue == -1)
fail( "Can't open message queue" );
// Send the parameter(arg 2)
mq_send( serverQueue, para, strlen( para ), 0 );
// Send the word (arg 3)
mq_send( serverQueue, word, strlen( word ), 0 );
//receive the answer from server
char ans[MESSAGE_LIMIT];
int len = mq_receive(clientQueue, ans, sizeof( ans ), NULL);
if(len >=0){
for ( int i = 0; i < len; i++ )
printf( "%c", ans[ i ] );
printf("\n");
}
// We're done, close our copy of the queue (which would happen when we exit anyway).
mq_close( serverQueue );
mq_close( clientQueue );
exit(0);
}